home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / smallt~1 / smallt~1.zoo / mstinterp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-26  |  100.5 KB  |  3,739 lines

  1. /***********************************************************************
  2.  *
  3.  *    Byte Code Interpreter Module.
  4.  *
  5.  *    Interprets the compiled bytecodes of a method.
  6.  *
  7.  ***********************************************************************/
  8.  
  9. /***********************************************************************
  10.  *
  11.  * Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  12.  * Written by Steve Byrne.
  13.  *
  14.  * This file is part of GNU Smalltalk.
  15.  *
  16.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  17.  * under the terms of the GNU General Public License as published by the Free
  18.  * Software Foundation; either version 1, or (at your option) any later 
  19.  * version.
  20.  * 
  21.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  22.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  23.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  24.  * more details.
  25.  * 
  26.  * You should have received a copy of the GNU General Public License along with
  27.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  28.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  29.  *
  30.  ***********************************************************************/
  31.  
  32. /*
  33.  *    Change Log
  34.  * ============================================================================
  35.  * Author      Date       Change
  36.  * sbyrne    20 May 90      Improved error handling when error: or
  37.  *              doesNotUnderstand: occurs.  Also, added ^C handling
  38.  *              to abort execution.
  39.  *
  40.  * sbyrne    24 Apr 90      Improved error handling for fopen/popen primitives.
  41.  *
  42.  * sbyrne    20 Apr 90      Make fileIn not close the stream that it's reading
  43.  *              from; this is taken care of by the caller, and causes
  44.  *              very strange behavior if we try to close it twice!
  45.  *
  46.  * sbyrne     7 Apr 90      Added declaration tracing primitive.
  47.  *
  48.  * sbyrne     7 Apr 90      Fixed fileIn: to check for existence of the file
  49.  *              before trying to open it.  Returns failure if the
  50.  *              file cannot be accessed.
  51.  *
  52.  * sbyrne    25 Mar 90      Minor change for AtariSt: decrease size of ASYNC
  53.  *              queue size.
  54.  *
  55.  * sbyrne    19 Dec 89      Added suport for primitive filein (for use with
  56.  *              autoload --
  57.  *                              "12 gauge autoloader"
  58.  *                              A. Swartzenegger
  59.  *                              The Terminator)
  60.  *
  61.  * sbyrne     2 Sep 89      Process primitives in and working...starting to
  62.  *              switch to compiled methods with descriptor instance
  63.  *              variable in addition to header.
  64.  *
  65.  * sbyrne     9 Aug 89      Conversion completed.  Performance now 40k
  66.  *              bytecodes/sec; was 43k bytecodes/sec.
  67.  *
  68.  * sbyrne    18 Jul 89      Began conversion from stack based method contexts and
  69.  *              blocks to more traditional method contexts and
  70.  *              blocks.  This change was done 1) to make call in from
  71.  *              C easier, 2) to make processs possible (they could
  72.  *              have been implemented using stack based contexts, but
  73.  *              somewhat space-wastefully), and 3) to conform with
  74.  *              the more traditional definition method contexts and
  75.  *              block contexts.
  76.  *
  77.  * sbyrne    26 May 89      added method cache!  Why didn't I spend the 1/2 hour
  78.  *              sooner?
  79.  *
  80.  * sbyrne     7 Jan 89      Created.
  81.  *
  82.  */
  83.  
  84.  
  85. #include "mst.h"
  86. #include "mstinterp.h"
  87. #include "mstdict.h"
  88. #include "mstsym.h"
  89. #include "mstoop.h"
  90. #include "mstsave.h"
  91. #include "mstcomp.h"
  92. #include "mstcint.h"
  93. #include "mstsysdep.h"
  94. #include <math.h>
  95. #include <stdio.h>
  96. #include <signal.h>
  97. #include <sys/time.h>
  98. #include <sys/types.h>
  99. #include <sys/stat.h>
  100. #include <setjmp.h>
  101.  
  102. #define    METHOD_CACHE_SIZE        (1 << 11) /* 2048, mostly random choice */
  103. #ifdef atarist
  104. #define ASYNC_QUEUE_SIZE        16 /* still way too much */
  105. #else
  106. #define ASYNC_QUEUE_SIZE        100 /* way too much */
  107. #endif
  108.  
  109. /* Don't enable this...it doesn't really work properly with the new GC, since
  110.    using local register variables defeats the ability to copy the root set
  111.    when a GC flip occurs. */
  112. /*#define LOCAL_REGS */
  113.  
  114. /* Enabling this means that some accessors for method object pieces, such
  115.    as instance variables or literals, are implemented as routines, instead
  116.    of being in-line code via macros */
  117. /* #define ACCESSOR_DEBUGGING */
  118.  
  119. #ifdef LOCAL_REGS
  120. #define exportSP()    *spAddr = sp
  121. #define exportIP()    *ipAddr = ip
  122. #define exportRegs()    { exportSP(); exportIP(); }
  123. #define importSP()    sp = *spAddr
  124. #define importIP()    ip = *ipAddr
  125. #define importRegs()    { importSP(); importIP(); }
  126. #else
  127. #define exportSP()
  128. #define exportIP()
  129. #define exportRegs()
  130. #define importSP()
  131. #define importIP()
  132. #define importRegs()
  133. #endif /* LOCAL_REGS */
  134.  
  135. #ifndef ACCESSOR_DEBUGGING
  136.  
  137. #define receiverVariableInternal(receiver, index) \
  138.   (!inBounds(receiver, index) && errorf("Index out of bounds %d", index), \
  139.     oopToObj(receiver)->data[index])
  140.  
  141. #define getStackReceiverInternal(numArgs) \
  142.   (stackAt(numArgs))
  143.  
  144. #define methodTemporaryInternal(index) \
  145.   (temporaries[index])
  146.  
  147.  
  148. #define methodLiteralInternal(methodOOP, index) \
  149.   (((Method)oopToObj(methodOOP))->literals[index])
  150.  
  151. #define methodVariableInternal(methodOOP, index) \
  152.   (associationValue(((Method)oopToObj(methodOOP))->literals[index]))
  153.  
  154. #define getMethodByteCodesInternal(methodOOP) \
  155.   (isNil(methodOOP) ? (Byte *)nil \
  156.    : ((Byte *)&((Method)oopToObj(methodOOP))->literals[((Method)oopToObj(methodOOP))->header.numLiterals]))
  157.  
  158.  
  159. #define getMethodHeaderInternal(methodOOP) \
  160.   (((Method)oopToObj(methodOOP))->header)
  161.  
  162. #define getMethodClassInternal(methodOOP) \
  163.   (associationValue(((Method)oopToObj(methodOOP))->literals[((Method)oopToObj(methodOOP))->header.numLiterals - 1]))
  164.  
  165.  
  166. #define storeReceiverVariableInternal(receiver, index, oop) \
  167. {  \
  168.   OOP __storeRecVarOOP = (oop); \
  169.   if (!inBounds(receiver, index)) { \
  170.     errorf("Index out of bounds %d", index); \
  171.   } \
  172.   prepareToStore(receiver, __storeRecVarOOP); \
  173.   oopToObj(receiver)->data[index] = __storeRecVarOOP; \
  174. }
  175.  
  176. #define storeMethodTemporaryInternal(index, oop) \
  177. { \
  178.   OOP __storeMethTempOOP = (oop); \
  179.   prepareToStore(thisContextOOP, __storeMethTempOOP); \
  180.   temporaries[index] = __storeMethTempOOP; \
  181. }
  182.  
  183. #define storeMethodVariableInternal(methodOOP, index, oop) \
  184.   setAssociationValue(((Method)oopToObj(methodOOP))->literals[index], oop)
  185.  
  186. #define storeMethodLiteralInternal(methodOOP, index, oop) \
  187. { \
  188.   OOP __storeMethLitOOP = (oop); \
  189.   prepareToStore(methodOOP, __storeMethLitOOP); \
  190.   ((Method)oopToObj(methodOOP))->literals[index] = __storeMethLitOOP; \
  191. }
  192.  
  193. #define inBoundsInternal(oop, index) \
  194.   ((index) >= 0 && (index) < numOOPs(oopToObj(oop)))
  195.  
  196. #define isBlockContextInternal(contextOOP) \
  197.   (oopClass(contextOOP) == blockContextClass)
  198.  
  199.  
  200. #define receiverVariable        receiverVariableInternal
  201. #define getStackReceiver        getStackReceiverInternal
  202. #define methodTemporary            methodTemporaryInternal
  203. #define methodVariable            methodVariableInternal
  204. #define getMethodByteCodes        getMethodByteCodesInternal
  205. #define getMethodClass            getMethodClassInternal
  206. #define storeReceiverVariable        storeReceiverVariableInternal
  207. #define storeMethodTemporary        storeMethodTemporaryInternal
  208. #define storeMethodVariable        storeMethodVariableInternal
  209. #define inBounds            inBoundsInternal
  210. #define isBlockContext            isBlockContextInternal
  211.  
  212. #define methodLiteral            methodLiteralInternal
  213. #define getMethodHeader            getMethodHeaderInternal
  214. #define storeMethodLiteral        storeMethodLiteralInternal
  215. #endif /* !ACCESSOR_DEBUGGING */
  216.  
  217.  
  218.  
  219. typedef enum {
  220.   openFilePrim,
  221.   closeFilePrim,
  222.   getCharPrim,
  223.   putCharPrim,
  224.   seekPrim,
  225.   tellPrim,
  226.   eofPrim,
  227.   popenFilePrim,
  228.   sizePrim
  229. } filePrimitiveTypes;
  230.  
  231. typedef struct FileStreamStruct {
  232.   OOP        collection;
  233.   OOP        ptr;
  234.   OOP        endPtr;
  235.   OOP        maxSize;
  236.   OOP        file;
  237.   OOP        name;
  238. } *FileStream;
  239.  
  240. typedef struct CompiledMethodStruct *Method;
  241.  
  242. typedef struct MethodContextStruct {
  243.   OBJ_HEADER;
  244.   OOP        sender;
  245.   OOP        ipOffset;    /* an integer byte index into method */
  246.   OOP        spOffset;    /* an integer index into cur context stack */
  247.   OOP        method;        /* the method that we're executing */
  248.   OOP        hasBlock;    /* nil or not nil */
  249.   OOP        selector;    /* the selector that invoked this method */
  250.   OOP        receiver;    /* the Receiver OOP */
  251.   OOP        contextStack[CONTEXT_STACK_SIZE];
  252. } *MethodContext;
  253.  
  254. typedef struct BlockContextStruct {
  255.   OBJ_HEADER;
  256.   OOP        caller;
  257.   OOP        ipOffset;    /* an integer byte index into method */
  258.   OOP        spOffset;    /* an integer index into cur context stack */
  259.   OOP        numArgs;    /* number of arguments we have */
  260.   OOP        initialIP;    /* initial value of IP (an offset) */
  261.   OOP        selector;    /* the selector that invoked this block */
  262.   OOP        home;        /* the home context */
  263.   OOP        contextStack[CONTEXT_STACK_SIZE];
  264. } *BlockContext;
  265.  
  266. typedef struct SemaphoreStruct {
  267.   OBJ_HEADER;
  268.   OOP        firstLink;
  269.   OOP        lastLink;
  270.   OOP        signals;
  271. } *Semaphore;
  272.  
  273. typedef struct ProcessStruct {
  274.   OBJ_HEADER;
  275.   OOP        nextLink;
  276.   OOP        suspendedContext;
  277.   OOP        priority;
  278.   OOP        myList;
  279. } *Process;
  280.  
  281. typedef struct ProcessorSchedulerStruct {
  282.   OBJ_HEADER;
  283.   OOP        processLists;
  284.   OOP        activeProcess;
  285. } *ProcessorScheduler;
  286.  
  287. long            byteCodeCounter;
  288. long            cacheHits = 0;
  289. long            cacheMisses = 0;
  290.  
  291. /* If this is true, for each byte code that is executed, the byte index
  292.  * within the current CompiledMethod and a decoded interpretation of
  293.  * the byte code is printed on standard output. */
  294. Boolean            executionTracing;
  295.  
  296. /* When this is true, and an interrupt occurs (such as SIGSEGV), Smalltalk
  297.  * will terminate itself by making a core dump (normally it does not
  298.  * terminate in this manner). */
  299. Boolean            makeCoreFile = false;
  300.  
  301.  
  302. Byte            *ip, **ipAddr = &ip;
  303. OOP            *sp, **spAddr = &sp;
  304. OOP            thisMethod;
  305.  
  306. int            collide[METHOD_CACHE_SIZE];
  307.  
  308. #ifdef countingByteCodes
  309. static long        byteCodes[256];
  310. static long        primitives[256];
  311. #endif
  312.  
  313. static OOP        methodCacheSelectors    [METHOD_CACHE_SIZE];
  314. static OOP        primitiveCacheSelectors [METHOD_CACHE_SIZE];
  315. static OOP        methodCacheClasses      [METHOD_CACHE_SIZE];
  316. static OOP        primitiveCacheClasses   [METHOD_CACHE_SIZE];
  317. static OOP        methodCacheMethods      [METHOD_CACHE_SIZE];
  318. static int        primitiveCachePrimitives[METHOD_CACHE_SIZE];
  319.  
  320. static OOP        queuedAsyncSignals    [ASYNC_QUEUE_SIZE]; 
  321. static int        asyncQueueIndex;
  322. static OOP        switchToProcess; /* non-nil when proc switch wanted */
  323.  
  324. static OOP        *temporaries;    /* points into method or block context
  325.                      * to start of arguments and
  326.                      * temporaries */
  327. static OOP        self;
  328. static OOP        thisContextOOP;
  329. static Boolean        inInterpreter = false;
  330. static int        exceptFlag;
  331.  
  332. /* When true, this causes the byte code interpeter to immediately act
  333.  * as if it saw a stream af method return bytecodes, until it finally exits.
  334.  */
  335. static Boolean        abortExecution = false;
  336.  
  337. /* When this is true, it means that the system is executing external C code,
  338.  * which can be used by the ^C handler to know whether it longjmp to the
  339.  * end of the C callout primitive in executePrimitiveOperation. */
  340. static Boolean        inCCode = false;
  341.  
  342.  
  343. /* Used to handle the case when the user types a ^C while executing callout
  344.  * code */
  345. static jmp_buf cCalloutJmpBuf;
  346.  
  347. /* when this flag is on and execution tracing is in effect, the top
  348.  * of the stack is printed as well as the byte code */
  349. static Boolean        verboseExecTracing = false;
  350.  
  351.  
  352. #ifdef ACCESSOR_DEBUGGING
  353. static OOP        methodTemporary(), receiverVariable(),
  354.               methodVariable(), getMethodClass(),
  355.             getStackReceiver(), methodLiteral();
  356. static void        storeMethodTemporary(), storeReceiverVariable(),
  357.               storeMethodVariable(), storeMethodLiteral();
  358. static Boolean        inBounds(), isBlockContext();
  359. static Byte        *getMethodByteCodes();
  360. static MethodHeader    getMethodHeader();
  361. #endif /* ACCESSOR_DEBUGGING */
  362. static OOP        findMethod(), createMethodContext(), 
  363.             getMethodContext(), getActiveProcess(),
  364.             getProcessLists(), highestPriorityProcess(),
  365.             removeFirstLink(), semaphoreNew();
  366. static void        returnWithValue(), 
  367.             sendBlockValue(),
  368.             showBacktrace(),
  369.             invalidateMethodCache(), methodHasBlockContext(),
  370.             sleepProcess(), resumeProcess(), activateProcess(),
  371.             changeProcessContext(), addLastLink(),
  372.             suspendActiveProcess(), moveSemaphoreOOPs();
  373. static Boolean        executePrimitiveOperation(),
  374.             noParentContext(), isEmpty(), isRealOOP();
  375. static char        *selectorAsString();
  376. static signalType    interruptHandler(), stopExecuting();
  377.  
  378. static OOP        *mathSelectors[16] = {
  379.   &plusSymbol,            /* 0  + */
  380.   &minusSymbol,            /* 1  - */
  381.   &lessThanSymbol,        /* 2  < */
  382.   &greaterThanSymbol,        /* 3  > */
  383.   &lessEqualSymbol,        /* 4  <= */
  384.   &greaterEqualSymbol,        /* 5  >= */
  385.   &equalSymbol,            /* 6  = */
  386.   ¬EqualSymbol,        /* 7  ~= */
  387.   ×Symbol,            /* 8  * */
  388.   ÷Symbol,        /* 9  / */
  389.   &remainderSymbol,        /* 10 \\ */
  390.   &plusSymbol,            /* 11 @, not implemented */
  391.   &bitShiftColonSymbol,        /* 12 bitShift: */
  392.   &integerDivideSymbol,        /* 13 // */
  393.   &bitAndColonSymbol,        /* 14 bitAnd: */
  394.   &bitOrColonSymbol        /* 15 bitOr: */
  395. };
  396.  
  397. struct SpecialSelectorStruct {
  398.   OOP        *selector;
  399.   int        args;
  400. } specialMessages[16] = {
  401.   &atColonSymbol,        1,
  402.   &atColonPutColonSymbol,    2,
  403.   &sizeSymbol,            0,
  404.   &nextSymbol,            0,
  405.   &nextPutColonSymbol,        1,
  406.   &atEndSymbol,            0,
  407.   &sameObjectSymbol,        1,
  408.   &classSymbol,            0,
  409.   &blockCopyColonSymbol,    1,
  410.   &valueSymbol,            0,
  411.   &valueColonSymbol,        1,
  412.   &doColonSymbol,        1,
  413.   &newSymbol,            0,
  414.   &newColonSymbol,        1,
  415.   &nilSymbol,            0, /* unimplemented selector */
  416.   &nilSymbol,            0  /* unimplemented selector */
  417. };
  418.  
  419. /*
  420.  
  421. ### This is from the old stack based context days...update it to reflect
  422. reality!
  423.  
  424. +-----------------------------------+
  425. | receiver (self)            |
  426. +-----------------------------------+
  427. | args                    |
  428. +-----------------------------------+
  429. | ...                    |
  430. +-----------------------------------+
  431. | temps                    |
  432. +-----------------------------------+
  433. | ...                    |
  434. +-----------------------------------+
  435. | saved ip of caller (relative)        | FP, SP on interpreter entry
  436. +-----------------------------------+
  437. | saved method of caller            |
  438. +-----------------------------------+
  439. | saved temp pointer of caller        |
  440. +-----------------------------------+
  441. | saved frame pointer of caller (?) |
  442. +-----------------------------------+
  443. | isBlock (boolean)                 |
  444. +-----------------------------------+
  445. | method context pointer        |
  446. +-----------------------------------+
  447. |                                   | SP (after saving state)
  448.  
  449.  */
  450.  
  451.  
  452. /*
  453.  Interpretation of the virtual machine byte codes
  454.  
  455. 0-15 push receiver variable     0000iiii
  456. 16-31 push temporary location    0001iiii
  457. 32-63 push literal constant    001iiiii
  458. 64-95 push literal variable    010iiiii
  459. 96-103 pop & store rec var    01100iii
  460. 104-111 pop & store temp loc    01101iii
  461. 112-119 push indexed        01110iii receiver true false nil -1 0 1 2
  462. 120-123 return indexed        011110ii receiver true false nil
  463. 124-125 return st top from    0111110i message, block
  464. 126-127 unused            0111111i
  465. 128    push indir        10000000 jjkkkkkk (receiver var, temp loc,
  466.                            lit const, lit var)
  467.                            [jj] #kkkkkk
  468. 129    store indir        10000001 jjkkkkkk (rv, tl, illegal, lv)
  469. 130    pop & store indir    10000010 jjkkkkkk (like store indir)
  470. 131    send lit selector    10000011 jjjkkkkk sel #kkkkk with jjj args
  471. 132    send lit selector    10000100 jjjjjjjj kkkkkkkk (as 131)
  472. 133    send lit sel to super    10000101 jjjkkkkk as 131
  473. 134    send lit to super    10000110 jjjjjjjj kkkkkkkk like 132
  474. 135    pop stack top        10000111
  475. 136    duplicate stack top    10001000
  476. 137    push active context    10001001
  477. 138-143    unused
  478. 144-151    jmp iii+1        10010iii
  479. 152-159    pop & jmp false iii+1    10011iii
  480. 160-167    jmp (iii-4)*256+jjjjjjjj10100iii jjjjjjjj
  481. 168-171 pop & jmp on true    101010ii jjjjjjjj ii*256+jjjjjjjj
  482. 172-175 pop & jmp on false    101011ii jjjjjjjj like 168
  483. 176-191 send arith message    1011iiii
  484. 192-207    send special message    1100iiii
  485. 208-223 send lit sel #iiii    1101iiii with no arguments
  486. 224-239 send lit sel #iiii    1110iiii with 1 argument
  487. 240-255 send lit sel #iiii    1111iiii with 2 arguments
  488. */
  489.  
  490. /*
  491.  *
  492.  * How the interpreter works:
  493.  *  1) The interpreter expects to be called in an environment where there
  494.  *     already exists a well-defined method context.  The instruction pointer,
  495.  *     stored in the global variable "ip", and the stack pointer, stored in the
  496.  *     global variable "sp", should be set up to point into the current
  497.  *     method and MethodContext.  Other global variables, such as "thisMethod",
  498.  *     "self", "temporaries", etc. should also be setup.  See the routine
  499.  *     prepareExecutionEnvironment for details.
  500.  *  2) The interpreter checks to see if any change in its state is required,
  501.  *     such as switching to a new process, dealing with an asynchronous signal
  502.  *     which is not yet implemented, and printing out the byte codes that are 
  503.  *     being executed, if that was requested by the user.
  504.  *  3) After that, the byte code that ip points to is fetched and decoded.
  505.  *     Some byte codes perform jumps, which are performed by merely adjusting
  506.  *     the value of ip.  Some are message sends, which are described in
  507.  *     more detail below.  Some instructions require more than one byte code
  508.  *     to perform their work; ip is advanced as needed and the extension
  509.  *     byte codes are fetched.
  510.  *  4) After dispatching the byte code, the interpreter loops around to
  511.  *     execute another byte code.  If ip has changed to point to nil, it is
  512.  *     a signal that the execution of the method is over, and the interpreter
  513.  *     returns to its caller.
  514.  *
  515.  * Note that the interpreter is not called recursively to implement message
  516.  * sends.  Rather the state of the interpreter is saved away in the currently
  517.  * executing context, and a new context is created and the global variables
  518.  * such as ip, sp, and temporaries are initialized accordingly.
  519.  *
  520.  * When a message send occurs, the sendMessage routine is invoked.  It 
  521.  * determines the class of the receiver, and checks to see if it already has
  522.  * cached the method definition for the given selector and receiver class.
  523.  * If so, that method is used, and if not, the receiver's method dictionary
  524.  * is searched for a method with the proper selector.  If it's not found in
  525.  * that method dictionary, the method dictionary of the classes parent is
  526.  * examined, and on up the hierarchy, until a matching selector is found.
  527.  *
  528.  * If no selector is found, the receiver is sent a #doesNotUnderstand: message
  529.  * to indicate that a matching method could not be found.
  530.  *
  531.  * If a method is found, it is examined for some special cases.  The special
  532.  * cases are primitive return of self, return of an instance variable, or
  533.  * execution of a primitive method definition.  This latter operation is
  534.  * performed by the executePrimitiveOperation routine.  If the execution
  535.  * of this primitive interpreter fails, the normal message send operation
  536.  * is performed.
  537.  *
  538.  * If the found method is not one of the special cases, or if it is a 
  539.  * primitive that failed to execute, a "normal" message send is performed.
  540.  * This basically entails saving away what state the interpreter has, such
  541.  * as the values of ip, and sp, being careful to save their relative locations
  542.  * and not their physical addresses, because one or more garbage collections
  543.  * could occur before the method context is returned to, and the absolute
  544.  * pointers would be invalid.
  545.  *
  546.  * The sendMessage routine then creates a new MethodContext object, makes
  547.  * its parent be the currently executing MethodContext, and sets up
  548.  * the interpreters global variables to reference the new method and
  549.  * new MethodContext.  Once those variables are set, sendMessage returns
  550.  * to the interpreter, which cheerfully begins executing the new method,
  551.  * totally unaware that the method that it was executing has changed.
  552.  *
  553.  * When a method returns, the method that called it is used to restore the
  554.  * interpreter's global variables to the state that they were in before
  555.  * the called method was called.  The values of ip and sp are restored to
  556.  * their absolute address values, and the other global state variables
  557.  * are restored accordingly.  When after the state has been restored, the
  558.  * interpreter continues execution, again totally oblivious to the fact
  559.  * that it's not running the same method it was on its previous byte code.
  560.  *
  561.  * Global state
  562.  * The following variables constitute the interpreter's state:
  563.  * ip -- the real memory address of the next byte code to be executed.
  564.  * sp -- the real memory address of the stack that's stored in the currently
  565.  *       executing block or method context.
  566.  * thisMethod -- a CompiledMethod that is the currently executing method.
  567.  * thisContextOOP -- a BlockContext or MethodContext that indicates the
  568.  *                   context that the interpreter is currently running in.
  569.  * temporaries -- physical address of the base of the method temporary
  570.  *                variables.  Typically a small number of bytes (multiple of 4
  571.  *                since it points to OOPs) lower than sp.
  572.  * self -- an OOP that is the current receiver of the current message.
  573.  * 
  574.  * Note about the interpreter:
  575.  * As an experiment, I unrolled the case statement somewhat into separate
  576.  * case arms for each byte code.  The intention was to increase performance.
  577.  * I haven't measured to see whether it makes a difference or not.
  578.  *
  579.  * The local regs concept was pre-GC.  By caching the values of IP and SP
  580.  * in local register variables, I hoped to increase performance.  I only
  581.  * needed to export the variables when I was calling out to routines that
  582.  * might change them.  However, the garbage collector may run at any time,
  583.  * and the values of IP and SP point to things in the root set and so will
  584.  * change on a GC flip.  I'm leaving the code to deal with them as local 
  585.  * registers in but conditionally compiled out until I can figure out a
  586.  * clever way to make them be registers again, or give up on the idea totally.
  587.  */
  588.  
  589. void interpret()
  590. {
  591.   Byte        ival, ival2, ival3, *savedIP;
  592.   OOP        returnedValue, *savedSP, methodContextOOP, tempOOP;
  593.   BlockContext    blockContext;
  594.   int        i;
  595.   IntState    oldSigMask;
  596. #ifdef LOCAL_REGS
  597.   register OOP    *sp;
  598.   register Byte    *ip;
  599. #endif /* LOCAL_REGS */
  600.  
  601.   importSP();
  602.   importIP();
  603.  
  604.   inInterpreter = true;
  605.  
  606.   exceptFlag = executionTracing;
  607.  
  608.   for (; ip; ) {        /* when IP is nil, return to caller */
  609.     clearGCFlipFlags();
  610.  
  611.     if (exceptFlag) {
  612.       if (abortExecution) {
  613.     goto abortMethod;    /* ugh! */
  614.       }
  615.       if (asyncQueueIndex) {    /* deal with any async signals  */
  616.     oldSigMask = disableInterrupts(); /* block out everything! */
  617.     for (i = 0; i < asyncQueueIndex; i++) {
  618.       /* ### this is not right...async signals must not allocate storage */
  619.       errorf("### Fix asyncSignal handling");
  620.       syncSignal(queuedAsyncSignals[i]);
  621.     }
  622.     asyncQueueIndex = 0;
  623.     enableInterrupts(oldSigMask);
  624.       }
  625.       if (!isNil(switchToProcess)) {
  626.     exportRegs();
  627.     changeProcessContext(switchToProcess);
  628.     importRegs();
  629.     /* make sure to validate the IP again */
  630.     continue;
  631.       }
  632.       if (executionTracing) {
  633.     printf("%5d:\t", relativeByteIndex(ip, thisMethod));
  634.     printByteCodeName(ip, relativeByteIndex(ip, thisMethod),
  635.               ((Method)oopToObj(thisMethod))->literals);
  636.     printf("\n");
  637.     if (verboseExecTracing) {
  638.       printf("\t  --> ");
  639.       printObject(stackTop());
  640.       printf("\n");
  641.     }
  642.       }
  643.       exceptFlag = executionTracing;
  644.     }
  645.       
  646.  
  647.     byteCodeCounter++;
  648. #ifdef countingByteCodes
  649.     byteCodes[*ip]++;
  650. #endif /* countingByteCodes */
  651.  
  652.     /* Note: some of the case arms are expanded out to literal cases,
  653.        instead of case0: case1: ... pushOOP(receiverVariable(self, ival&15))
  654.        this is an experiment to try to improve performance of the byte code
  655.        interpreter throughout the system. */
  656.     switch(ival = *ip++) {
  657.     case  0:    pushOOP(receiverVariable(self, 0));    break;
  658.     case  1:    pushOOP(receiverVariable(self, 1));    break;
  659.     case  2:    pushOOP(receiverVariable(self, 2));    break;
  660.     case  3:    pushOOP(receiverVariable(self, 3));    break;
  661.     case  4:    pushOOP(receiverVariable(self, 4));    break;
  662.     case  5:    pushOOP(receiverVariable(self, 5));    break;
  663.     case  6:    pushOOP(receiverVariable(self, 6));    break;
  664.     case  7:    pushOOP(receiverVariable(self, 7));    break;
  665.     case  8:    pushOOP(receiverVariable(self, 8));    break;
  666.     case  9:    pushOOP(receiverVariable(self, 9));    break;
  667.     case 10:    pushOOP(receiverVariable(self, 10));    break;
  668.     case 11:    pushOOP(receiverVariable(self, 11));    break;
  669.     case 12:    pushOOP(receiverVariable(self, 12));    break;
  670.     case 13:    pushOOP(receiverVariable(self, 13));    break;
  671.     case 14:    pushOOP(receiverVariable(self, 14));    break;
  672.     case 15:    pushOOP(receiverVariable(self, 15));    break;
  673.  
  674.     case 16:    pushOOP(methodTemporary(0));    break;
  675.     case 17:    pushOOP(methodTemporary(1));    break;
  676.     case 18:    pushOOP(methodTemporary(2));    break;
  677.     case 19:    pushOOP(methodTemporary(3));    break;
  678.     case 20:    pushOOP(methodTemporary(4));    break;
  679.     case 21:    pushOOP(methodTemporary(5));    break;
  680.     case 22:    pushOOP(methodTemporary(6));    break;
  681.     case 23:    pushOOP(methodTemporary(7));    break;
  682.     case 24:    pushOOP(methodTemporary(8));    break;
  683.     case 25:    pushOOP(methodTemporary(9));    break;
  684.     case 26:    pushOOP(methodTemporary(10));    break;
  685.     case 27:    pushOOP(methodTemporary(11));    break;
  686.     case 28:    pushOOP(methodTemporary(12));    break;
  687.     case 29:    pushOOP(methodTemporary(13));    break;
  688.     case 30:    pushOOP(methodTemporary(14));    break;
  689.     case 31:    pushOOP(methodTemporary(15));    break;
  690.  
  691.     case 32:    pushOOP(methodLiteral(thisMethod, 0));    break;
  692.     case 33:    pushOOP(methodLiteral(thisMethod, 1));    break;
  693.     case 34:    pushOOP(methodLiteral(thisMethod, 2));    break;
  694.     case 35:    pushOOP(methodLiteral(thisMethod, 3));    break;
  695.     case 36:    pushOOP(methodLiteral(thisMethod, 4));    break;
  696.     case 37:    pushOOP(methodLiteral(thisMethod, 5));    break;
  697.     case 38:    pushOOP(methodLiteral(thisMethod, 6));    break;
  698.     case 39:    pushOOP(methodLiteral(thisMethod, 7));    break;
  699.     case 40:    pushOOP(methodLiteral(thisMethod, 8));    break;
  700.     case 41:    pushOOP(methodLiteral(thisMethod, 9));    break;
  701.     case 42:    pushOOP(methodLiteral(thisMethod, 10));    break;
  702.     case 43:    pushOOP(methodLiteral(thisMethod, 11));    break;
  703.     case 44:    pushOOP(methodLiteral(thisMethod, 12));    break;
  704.     case 45:    pushOOP(methodLiteral(thisMethod, 13));    break;
  705.     case 46:    pushOOP(methodLiteral(thisMethod, 14));    break;
  706.     case 47:    pushOOP(methodLiteral(thisMethod, 15));    break;
  707.     case 48:    pushOOP(methodLiteral(thisMethod, 16));    break;
  708.     case 49:    pushOOP(methodLiteral(thisMethod, 17));    break;
  709.     case 50:    pushOOP(methodLiteral(thisMethod, 18));    break;
  710.     case 51:    pushOOP(methodLiteral(thisMethod, 19));    break;
  711.     case 52:    pushOOP(methodLiteral(thisMethod, 20));    break;
  712.     case 53:    pushOOP(methodLiteral(thisMethod, 21));    break;
  713.     case 54:    pushOOP(methodLiteral(thisMethod, 22));    break;
  714.     case 55:    pushOOP(methodLiteral(thisMethod, 23));    break;
  715.     case 56:    pushOOP(methodLiteral(thisMethod, 24));    break;
  716.     case 57:    pushOOP(methodLiteral(thisMethod, 25));    break;
  717.     case 58:    pushOOP(methodLiteral(thisMethod, 26));    break;
  718.     case 59:    pushOOP(methodLiteral(thisMethod, 27));    break;
  719.     case 60:    pushOOP(methodLiteral(thisMethod, 28));    break;
  720.     case 61:    pushOOP(methodLiteral(thisMethod, 29));    break;
  721.     case 62:    pushOOP(methodLiteral(thisMethod, 30));    break;
  722.     case 63:    pushOOP(methodLiteral(thisMethod, 31));    break;
  723.  
  724.     case 64:    pushOOP(methodVariable(thisMethod, 0));    break;
  725.     case 65:    pushOOP(methodVariable(thisMethod, 1));    break;
  726.     case 66:    pushOOP(methodVariable(thisMethod, 2));    break;
  727.     case 67:    pushOOP(methodVariable(thisMethod, 3));    break;
  728.     case 68:    pushOOP(methodVariable(thisMethod, 4));    break;
  729.     case 69:    pushOOP(methodVariable(thisMethod, 5));    break;
  730.     case 70:    pushOOP(methodVariable(thisMethod, 6));    break;
  731.     case 71:    pushOOP(methodVariable(thisMethod, 7));    break;
  732.     case 72:    pushOOP(methodVariable(thisMethod, 8));    break;
  733.     case 73:    pushOOP(methodVariable(thisMethod, 9));    break;
  734.     case 74:    pushOOP(methodVariable(thisMethod, 10));    break;
  735.     case 75:    pushOOP(methodVariable(thisMethod, 11));    break;
  736.     case 76:    pushOOP(methodVariable(thisMethod, 12));    break;
  737.     case 77:    pushOOP(methodVariable(thisMethod, 13));    break;
  738.     case 78:    pushOOP(methodVariable(thisMethod, 14));    break;
  739.     case 79:    pushOOP(methodVariable(thisMethod, 15));    break;
  740.     case 80:    pushOOP(methodVariable(thisMethod, 16));    break;
  741.     case 81:    pushOOP(methodVariable(thisMethod, 17));    break;
  742.     case 82:    pushOOP(methodVariable(thisMethod, 18));    break;
  743.     case 83:    pushOOP(methodVariable(thisMethod, 19));    break;
  744.     case 84:    pushOOP(methodVariable(thisMethod, 20));    break;
  745.     case 85:    pushOOP(methodVariable(thisMethod, 21));    break;
  746.     case 86:    pushOOP(methodVariable(thisMethod, 22));    break;
  747.     case 87:    pushOOP(methodVariable(thisMethod, 23));    break;
  748.     case 88:    pushOOP(methodVariable(thisMethod, 24));    break;
  749.     case 89:    pushOOP(methodVariable(thisMethod, 25));    break;
  750.     case 90:    pushOOP(methodVariable(thisMethod, 26));    break;
  751.     case 91:    pushOOP(methodVariable(thisMethod, 27));    break;
  752.     case 92:    pushOOP(methodVariable(thisMethod, 28));    break;
  753.     case 93:    pushOOP(methodVariable(thisMethod, 29));    break;
  754.     case 94:    pushOOP(methodVariable(thisMethod, 30));    break;
  755.     case 95:    pushOOP(methodVariable(thisMethod, 31));    break;
  756.  
  757.     case  96:    storeReceiverVariable(self, 0, popOOP());    break;
  758.     case  97:    storeReceiverVariable(self, 1, popOOP());    break;
  759.     case  98:    storeReceiverVariable(self, 2, popOOP());    break;
  760.     case  99:    storeReceiverVariable(self, 3, popOOP());    break;
  761.     case 100:    storeReceiverVariable(self, 4, popOOP());    break;
  762.     case 101:    storeReceiverVariable(self, 5, popOOP());    break;
  763.     case 102:    storeReceiverVariable(self, 6, popOOP());    break;
  764.     case 103:    storeReceiverVariable(self, 7, popOOP());    break;
  765.  
  766.     case 104:    storeMethodTemporary(0, popOOP());    break;
  767.     case 105:    storeMethodTemporary(1, popOOP());    break;
  768.     case 106:    storeMethodTemporary(2, popOOP());    break;
  769.     case 107:    storeMethodTemporary(3, popOOP());    break;
  770.     case 108:    storeMethodTemporary(4, popOOP());    break;
  771.     case 109:    storeMethodTemporary(5, popOOP());    break;
  772.     case 110:    storeMethodTemporary(6, popOOP());    break;
  773.     case 111:    storeMethodTemporary(7, popOOP());    break;
  774.  
  775.     case 112: uncheckedPushOOP(self);        break;
  776.     case 113: uncheckedPushOOP(trueOOP);    break;
  777.     case 114: uncheckedPushOOP(falseOOP);     break;
  778.     case 115: uncheckedPushOOP(nilOOP);     break;
  779.     case 116: pushInt(-1);            break;
  780.     case 117: pushInt(0);            break;
  781.     case 118: pushInt(1);            break;
  782.     case 119: pushInt(2);            break;
  783.  
  784.     case 120: case 121: case 122: case 123:
  785.       switch (ival & 3) {
  786.       case 0: uncheckedPushOOP(self);       break;
  787.       case 1: uncheckedPushOOP(trueOOP);    break;
  788.       case 2: uncheckedPushOOP(falseOOP);     break;
  789.       case 3: uncheckedPushOOP(nilOOP);     break;
  790.       }
  791.  
  792.       /* fall through */
  793.  
  794.     case 124:            /* return stack top from method */
  795. abortMethod:            /* here if ^C is seen to abort things */
  796.       returnedValue = popOOP();
  797.  
  798.       if (isBlockContext(thisContextOOP)) {
  799.     /*
  800.      * We're executing in a block context and an explicit return is
  801.      * encountered.  This means that we are to return from the caller of
  802.      * the method that created the block context, no matter how many
  803.      * levels of message sending are between where we currently are and
  804.      * our parent method context.
  805.      */
  806.     blockContext = (BlockContext)oopToObj(thisContextOOP);
  807.     methodContextOOP = blockContext->home;
  808.     if (noParentContext(methodContextOOP)) {
  809.       /* ### this should send a message to Object of some kind */
  810.       errorf("Block returning to non-existent method context");
  811.       return;
  812.     }
  813.       } else {
  814.     methodContextOOP = thisContextOOP;
  815.       }
  816.  
  817.       returnWithValue(returnedValue, methodContextOOP);
  818.       importRegs();        /* don't need to export these */
  819.       break;
  820.  
  821.     case 125:            /* return stack top from block to caller */
  822.       returnedValue = popOOP();
  823.       returnWithValue(returnedValue, thisContextOOP);
  824.       importRegs();
  825.       break;
  826.  
  827. /* 126, 127 unused by blue book, allocating 127 for debugger's
  828.    breakpoint (not yet implemented) */
  829.  
  830.     case 128:
  831.       ival2 = *ip++;
  832.       switch (ival2 >> 6) {
  833.       case 0:
  834.     pushOOP(receiverVariable(self, ival2 & 63));
  835.     break;
  836.       case 1:
  837.     pushOOP(methodTemporary(ival2 & 63));
  838.     break;
  839.       case 2:
  840.     pushOOP(methodLiteral(thisMethod, ival2 & 63));
  841.     break;
  842.       case 3:
  843.     pushOOP(methodVariable(thisMethod, ival2 & 63));
  844.     break;
  845.       }
  846.       break;
  847.  
  848.     case 129:
  849.       ival2 = *ip++;
  850.       switch (ival2 >> 6) {
  851.       case 0:
  852.     storeReceiverVariable(self, ival2 & 63, stackTop());
  853.     break;
  854.       case 1:
  855.     storeMethodTemporary(ival2 & 63, stackTop());
  856.     break;
  857.       case 2:
  858.     errorf("Attempt to store into a method constant");
  859.     break;
  860.       case 3:
  861.     storeMethodVariable(thisMethod, ival2 & 63, stackTop());
  862.       }
  863.       break;
  864.  
  865.     case 130:
  866.       ival2 = *ip++;
  867.       switch (ival2 >> 6) {
  868.       case 0:
  869.     storeReceiverVariable(self, ival2 & 63, popOOP());
  870.     break;
  871.       case 1:
  872.     storeMethodTemporary(ival2 & 63, popOOP());
  873.     break;
  874.       case 2:
  875.     errorf("Attempt to store into a method constant");
  876.     break;
  877.       case 3:
  878.     storeMethodVariable(thisMethod, ival2 & 63, popOOP());
  879.       }
  880.       break;
  881.  
  882.     case 131:            /* send selector y (xxxyyyyy), x args */
  883.       ival2 = *ip++;
  884.       /* ### Send message knows the number of arguments that are being
  885.      passed.  We could easily adjust the stack pointer here by doing
  886.      some kind of popNOOPs.  The only trouble is what happens when
  887.      the number of args doesn't agree with what the method is expecting,
  888.      and we have to generate an error.  Also, if we don't export the sp
  889.      here, we'll have to pass this as a parameter and sendMessage will
  890.      have to export it anyway.  The cost of an export or import is
  891.      about 1 or 2 instructions, so it may be cheap enough to just do
  892.      in the places that we need to to it */
  893.       exportRegs();        /* ### can this be removed? */
  894.       sendMessage(methodLiteral(thisMethod, ival2 & 31), ival2 >> 5, false);
  895.       importRegs();
  896.       break;
  897.  
  898.     case 132:            /* send selector y (xxxxxxxx,yyyyyyyy) x args*/
  899.       ival2 = *ip++;        /* the number of args */
  900.       ival3 = *ip++;        /* the selector */
  901.       exportRegs();
  902.       sendMessage(methodLiteral(thisMethod, ival3), ival2, false);
  903.       importRegs();
  904.       break;
  905.  
  906.     case 133:            /* send super selector y (xxxyyyyy), x args*/
  907.       ival2 = *ip++;
  908.       exportRegs();
  909.       sendMessage(methodLiteral(thisMethod, ival2 & 31), ival2 >> 5, true);
  910.       importRegs();
  911.       break;
  912.  
  913.     case 134:            /* send super y (xxxxxxxx,yyyyyyyy) x args */
  914.       ival2 = *ip++;        /* the number of args */
  915.       ival3 = *ip++;        /* the selector */
  916.       exportRegs();
  917.       sendMessage(methodLiteral(thisMethod, ival3), ival2, true);
  918.       importRegs();
  919.       break;
  920.  
  921.     case 135:
  922.       popOOP();
  923.       break;
  924.  
  925.     case 136:
  926.       tempOOP = stackTop();
  927.       pushOOP(tempOOP);
  928.       break;
  929.  
  930.     case 137:             /* push active context */
  931.       pushOOP(thisContextOOP);
  932.       break;
  933.  
  934.     case 144: case 145: case 146: case 147:
  935.     case 148: case 149: case 150: case 151:
  936.       ip += (ival & 7) + 1;    /* jump forward 1 to 8 bytes */
  937.       break;
  938.  
  939.     case 152: case 153: case 154: case 155:
  940.     case 156: case 157: case 158: case 159:
  941.       if (popOOP() == falseOOP) { /* jump forward if false 1 to 8 bytes */
  942.     ip += (ival & 7) + 1;
  943.       }
  944.       break;
  945.  
  946.     case 160: case 161: case 162: case 163:
  947.     case 164: case 165: case 166: case 167:
  948.       ival2 = *ip++;        /* jump forward or back */
  949.       ip += (((ival & 7) - 4) << 8) + ival2;
  950.       break;
  951.  
  952.     case 168: case 169: case 170: case 171:
  953.       ival2 = *ip++;
  954.       if (popOOP() == trueOOP) {
  955.     ip += ((ival & 3) << 8) + ival2;
  956.       }
  957.       break;
  958.  
  959.     case 172: case 173: case 174: case 175:
  960.       ival2 = *ip++;
  961.       if (popOOP() == falseOOP) {
  962.     ip += ((ival & 3) << 8) + ival2;
  963.       }
  964.       break;
  965.  
  966.     case 176: case 177: case 178: case 179:
  967.     case 180: case 181: case 182: case 183:
  968.     case 184: case 185: case 186: case 187:
  969.     case 188: case 189: case 190: case 191:
  970.                 /* send math message */
  971.       exportRegs();
  972.       sendMessage(*mathSelectors[ival & 15], 1, false);
  973.       importRegs();
  974.       break;
  975.  
  976.     case 192: case 193: case 194: case 195:
  977.     case 196: case 197: case 198: case 199:
  978.     case 200: case 201: case 202: case 203:
  979.     case 204: case 205: case 206: case 207:
  980.                 /* send special message */
  981.       exportRegs();
  982.       sendMessage(*specialMessages[ival & 15].selector,
  983.           specialMessages[ival & 15].args, false);
  984.       importRegs();
  985.       break;
  986.  
  987.     case 208: case 209: case 210: case 211:
  988.     case 212: case 213: case 214: case 215:
  989.     case 216: case 217: case 218: case 219:
  990.     case 220: case 221: case 222: case 223:
  991.                 /* send selector no args */
  992.       exportRegs();
  993.       sendMessage(methodLiteral(thisMethod, ival & 15), 0, false);
  994.       importRegs();
  995.       break;
  996.  
  997.     case 224: case 225: case 226: case 227:
  998.     case 228: case 229: case 230: case 231:
  999.     case 232: case 233: case 234: case 235:
  1000.     case 236: case 237: case 238: case 239:
  1001.                 /* send selector 1 arg */
  1002.       exportRegs();
  1003.       sendMessage(methodLiteral(thisMethod, ival & 15), 1, false);
  1004.       importRegs();
  1005.       break;
  1006.  
  1007.     case 240: case 241: case 242: case 243:
  1008.     case 244: case 245: case 246: case 247:
  1009.     case 248: case 249: case 250: case 251:
  1010.     case 252: case 253: case 254: case 255:
  1011.                 /* send selector 2 args */
  1012.       exportRegs();
  1013.       sendMessage(methodLiteral(thisMethod, ival & 15), 2, false);
  1014.       importRegs();
  1015.       break;
  1016.  
  1017.     default:
  1018.       errorf("Illegal byte code %d executed\n", ival);
  1019.       break;
  1020.     }
  1021.   }
  1022.   inInterpreter = false;
  1023.  
  1024.   exportRegs();
  1025. }
  1026.  
  1027. static void changeProcessContext(newProcess)
  1028. OOP    newProcess;
  1029. {
  1030.   MethodContext thisContext, methodContext;
  1031.   OOP        processOOP, methodContextOOP;
  1032.   Process    process;
  1033.   ProcessorScheduler processor;
  1034.   
  1035.   switchToProcess = nilOOP;
  1036.   if (!isNil(thisContextOOP)) {
  1037.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1038.     /* save old context information */
  1039.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  1040.     /* leave sp pointing to receiver, which is replaced on return with value*/
  1041.     thisContext->spOffset = fromInt(sp - thisContext->contextStack);
  1042.   }
  1043.  
  1044.   processOOP = getActiveProcess();
  1045.   process = (Process)oopToObj(processOOP);
  1046.   prepareToStore(processOOP, thisContextOOP);
  1047.   process->suspendedContext = thisContextOOP;
  1048.  
  1049.   processor = (ProcessorScheduler)oopToObj(processorOOP);
  1050.   prepareToStore(processorOOP, newProcess);
  1051.   processor->activeProcess = newProcess;
  1052.   
  1053.   process = (Process)oopToObj(newProcess);
  1054.  
  1055.   thisContextOOP = process->suspendedContext;
  1056.   /* ### should this be block context? */
  1057.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1058.  
  1059.   methodContextOOP = getMethodContext(thisContextOOP);
  1060.  
  1061.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1062.   thisMethod = methodContext->method;
  1063.   ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  1064.   sp = thisContext->contextStack + toInt(thisContext->spOffset);
  1065.  
  1066.   /* temporaries and self live in the method, not in the block */
  1067.   temporaries = methodContext->contextStack;
  1068.   self = methodContext->receiver;
  1069. }
  1070.  
  1071.  
  1072. /*
  1073.  *    static Boolean noParentContext(methodContextOOP)
  1074.  *
  1075.  * Description
  1076.  *
  1077.  *    Returns true if there is no parent context for "methodContextOOP".
  1078.  *    This occurs when the method context has been returned from, but it had
  1079.  *    created a block context during its execution and so it was not
  1080.  *    deallocated when it returned.  Now some block context is trying to
  1081.  *    return from that method context, but where to return to is undefined.
  1082.  *
  1083.  * Inputs
  1084.  *
  1085.  *    methodContextOOP: 
  1086.  *        An OOP that is the method context to be examined.
  1087.  *
  1088.  * Outputs
  1089.  *
  1090.  *    True if the current method has no parent, false otherwise.
  1091.  */
  1092. static Boolean noParentContext(methodContextOOP)
  1093. OOP methodContextOOP;
  1094. {
  1095.   MethodContext methodContext;
  1096.  
  1097.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1098.  
  1099.   return (isNil(methodContext->sender));
  1100. }
  1101.  
  1102. /*
  1103.  *    static OOP getMethodContext(contextOOP)
  1104.  *
  1105.  * Description
  1106.  *
  1107.  *    Returns the method context for either a block context or a method
  1108.  *    context. 
  1109.  *
  1110.  * Inputs
  1111.  *
  1112.  *    contextOOP: Block or Method context OOP
  1113.  *        
  1114.  *
  1115.  * Outputs
  1116.  *
  1117.  *    Method context for CONTEXTOOP.
  1118.  */
  1119. static OOP getMethodContext(contextOOP)
  1120. OOP    contextOOP;
  1121. {
  1122.   BlockContext blockContext;
  1123.  
  1124.   if (isBlockContext(contextOOP)) {
  1125.     blockContext = (BlockContext)oopToObj(contextOOP);
  1126.     return (blockContext->home);
  1127.   } else {
  1128.     return (contextOOP);
  1129.   }
  1130. }
  1131.  
  1132. static OOP allocMethodContext()
  1133. {
  1134.   MethodContext methodContext;
  1135.  
  1136.   methodContext = (MethodContext)instantiateWith(methodContextClass,
  1137.                          CONTEXT_STACK_SIZE);
  1138.   return (allocOOP(methodContext));
  1139. }
  1140.  
  1141. static OOP allocBlockContext()
  1142. {
  1143.   BlockContext blockContext;
  1144.  
  1145.   blockContext = (BlockContext)instantiateWith(blockContextClass,
  1146.                            CONTEXT_STACK_SIZE);
  1147.   return (allocOOP(blockContext));
  1148. }
  1149.  
  1150.  
  1151. #ifdef ACCESSOR_DEBUGGING
  1152. /*
  1153.  *    static Boolean isBlockContext(contextOOP)
  1154.  *
  1155.  * Description
  1156.  *
  1157.  *    Returns true if "contextOOP" is a block context.
  1158.  *
  1159.  * Inputs
  1160.  *
  1161.  *    contextOOP: 
  1162.  *        an OOP for a context that is to be checked.
  1163.  *
  1164.  * Outputs
  1165.  *
  1166.  *    True if it's a block context, false otherwise.
  1167.  */
  1168. static Boolean isBlockContext(contextOOP)
  1169. OOP    contextOOP;
  1170. {
  1171.   return (oopClass(contextOOP) == blockContextClass);
  1172. }
  1173. #endif /* ACCESSOR_DEBUGGING */
  1174.  
  1175.  
  1176. /*
  1177.  * on entry to this routine, the stack should have the receiver and the
  1178.  * arguments pushed on the stack.  We need to get a new context,
  1179.  * setup things like the IP, SP, and Temporary pointers, and then
  1180.  * return.   Note that this routine DOES NOT invoke the interpreter; it merely
  1181.  * sets up a new context so that calling (or, more typically, returning to) the
  1182.  * interpreter will operate properly.  This kind of sending is for normal
  1183.  * messages only.  Things like sending a "value" message to a block context are
  1184.  * handled by primitives which do similar things, but they use information from
  1185.  * the block and method contexts that we don't have available (or need) here.
  1186.  */
  1187.  
  1188. void sendMessage(sendSelector, sendArgs, sendToSuper)
  1189. OOP    sendSelector;
  1190. int    sendArgs;
  1191. Boolean    sendToSuper;
  1192. {
  1193.   OOP        methodOOP, receiver, methodClass, receiverClass,
  1194.         argsArray, newContextOOP;
  1195.   MethodContext thisContext, newContext;
  1196.   MethodHeader    header;
  1197.   int        i;
  1198.   long        hashIndex;
  1199.  
  1200.   if (!sendToSuper) {
  1201.     receiver = getStackReceiver(sendArgs);
  1202.     if (isInt(receiver)) {
  1203.       receiverClass = integerClass;
  1204.     } else {
  1205.       receiverClass = oopClass(receiver);
  1206.     }
  1207.   } else {
  1208.     methodClass = getMethodClass(thisMethod);
  1209.     receiverClass = superClass(methodClass);
  1210.     receiver = self;
  1211.   }
  1212.  
  1213.   /* hash the selector and the class of the receiver together using XOR.
  1214.    * Since both are pointers to long word aligned quantities, shift over
  1215.    * by 2 bits to remove the useless low order zeros */
  1216.   hashIndex = ((long)sendSelector ^ (long)receiverClass) >> 4;
  1217.   hashIndex &= (METHOD_CACHE_SIZE - 1);
  1218.  
  1219.  
  1220.   if (methodCacheSelectors[hashIndex] == sendSelector
  1221.       && methodCacheClasses[hashIndex] == receiverClass) {
  1222.     /* :-) CACHE HIT!!! (-: */
  1223.     methodOOP = methodCacheMethods[hashIndex];
  1224.     cacheHits++;
  1225.   } else {
  1226.     /* :-( cache miss )-: */
  1227.     methodOOP = findMethod(receiverClass, sendSelector, &methodClass);
  1228.     if (isNil(methodOOP)) {
  1229.       argsArray = arrayNew(sendArgs);
  1230.       for (i = 0; i < sendArgs; i++) {
  1231.     arrayAtPut(argsArray, i+1, stackAt(sendArgs-i-1));
  1232.       }
  1233.       popNOOPs(sendArgs);
  1234.       pushOOP(messageNewArgs(sendSelector, argsArray));
  1235.       sendMessage(doesNotUnderstandColonSymbol, 1, false);
  1236.       return;
  1237.     }
  1238.     methodCacheSelectors[hashIndex] = sendSelector;
  1239.     methodCacheClasses[hashIndex] = receiverClass;
  1240.     methodCacheMethods[hashIndex] = methodOOP;
  1241.     collide[hashIndex]++;
  1242.     cacheMisses++;
  1243.   }
  1244.  
  1245.   header = getMethodHeader(methodOOP);
  1246.   if (header.numArgs != sendArgs) {
  1247.     errorf("invalid number of arguments %d, expecting %d", sendArgs,
  1248.        header.numArgs);
  1249.     return;
  1250.   }
  1251.  
  1252.   if (header.headerFlag != 0) {
  1253.     switch (header.headerFlag) {
  1254.     case 1:            /* return self */
  1255.       if (sendArgs != 0) {
  1256.     errorf("method returns primitive self, but has args!!!");
  1257.     return;
  1258.       }
  1259.  
  1260.       /* self is already on the stack...so we leave it */
  1261.       return;
  1262.  
  1263.     case 2:            /* return instance variable */
  1264.       if (sendArgs != 0) {
  1265.     errorf("method returns primitive instance variable, but has args!!!");
  1266.     return;
  1267.       }
  1268.       /* replace receiver with the returned instance variable */
  1269.       setStackTop(receiverVariable(receiver, header.numTemps));
  1270.       return;
  1271.  
  1272.     case 3:            /* send primitive */
  1273.       if (!executePrimitiveOperation(header.primitiveIndex, sendArgs,
  1274.                      methodOOP)) {
  1275.     return;
  1276.       }
  1277.       /* primitive failed.  Invoke the normal method */
  1278.       break;
  1279.     }
  1280.   }
  1281.  
  1282.   if (!isNil(thisContextOOP)) {
  1283.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1284.     /* save old context information */
  1285.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  1286.     /* leave sp pointing to receiver, which is replaced on return with value*/
  1287.     thisContext->spOffset = fromInt(sp - sendArgs - thisContext->contextStack);
  1288.   }
  1289.  
  1290.   /* prepare the new state */
  1291.   newContextOOP = allocMethodContext();
  1292.   newContext = (MethodContext)oopToObj(newContextOOP);
  1293.   newContext->sender = thisContextOOP;
  1294.   maybeMoveOOP(methodOOP);
  1295.   newContext->method = methodOOP;
  1296.   newContext->hasBlock = nilOOP;    /* becomes non-nil when a block is created */
  1297.  
  1298.   /* copy self and sendArgs arguments into new context */
  1299.   maybeMoveOOP(sendSelector);
  1300.   newContext->selector = sendSelector;
  1301.   maybeMoveOOP(receiver);
  1302.   newContext->receiver = receiver;
  1303.   memcpy(newContext->contextStack, &sp[-sendArgs+1], (sendArgs) * sizeof(OOP));
  1304.   for (i = 0; i < sendArgs; i++) {
  1305.     maybeMoveOOP(newContext->contextStack[i]);
  1306.   }
  1307.  
  1308.   sp = &newContext->contextStack[sendArgs + header.numTemps - 1];
  1309.                 /* 1 before the actual start of stack */
  1310.  
  1311.   thisMethod = methodOOP;
  1312.   thisContextOOP = newContextOOP;
  1313.   
  1314.   temporaries = newContext->contextStack;
  1315.   self = newContext->receiver;
  1316.   ip = getMethodByteCodes(thisMethod);
  1317.   /* ### fix getmethodbytecodes to check for actual byte codes in method */
  1318. }
  1319.  
  1320.  
  1321. /*
  1322.  *    static void returnWithValue(returnedValue, returnContext)
  1323.  *
  1324.  * Description
  1325.  *
  1326.  *    Return from context "returnContext" with value "returnedValue".  Note
  1327.  *    that this context may not be the current context.  If returnContext
  1328.  *    is not a block context, then we need to carefully unwind the
  1329.  *    "method call stack".  Here carefully means that we examine each
  1330.  *    context.  If it's a block context then we cannot deallocate it.  If
  1331.  *    it's a method context, and if during its execution it did not create a
  1332.  *    block context, then we can deallocate it.  Otherwise, we need to mark
  1333.  *    it as returned (set the sender to nilOOP) and continue up the call
  1334.  *    chain until we reach returnContext.
  1335.  *
  1336.  * Inputs
  1337.  *
  1338.  *    returnedValue: 
  1339.  *        Value to be put on the stack in the sender's context.
  1340.  *    returnContext: 
  1341.  *        The context to return from, an OOP.  This may not be the
  1342.  *        current context.
  1343.  *
  1344.  */
  1345. static void returnWithValue(returnedValue, returnContext)
  1346. OOP    returnedValue, returnContext;
  1347. {
  1348.   MethodContext    oldContext, thisContext, methodContext;
  1349.   OOP            oldContextOOP, methodContextOOP;
  1350.  
  1351.   while (thisContextOOP != returnContext) {
  1352.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  1353.     if (isBlockContext(thisContextOOP)) {
  1354.       thisContextOOP = ((BlockContext)thisContext)->caller;
  1355.     } else {
  1356.       oldContextOOP = thisContextOOP;
  1357.       thisContextOOP = thisContext->sender; /* ### what if sender is nil? */
  1358.       if (!isNil(thisContext->hasBlock)) {
  1359.     /* This context created a block.  Since we don't know who is holding
  1360.        the block, we must presume that it is global.  Since any blocks
  1361.        created by this method can reference arguments and temporaries
  1362.        of this method, we must keep the method context around, but mark
  1363.        it as non-returnable so that attempts to return from it to an
  1364.        undefined place will lose. */
  1365.     thisContext->sender = nilOOP;
  1366.       }
  1367.     }
  1368.   }
  1369.  
  1370.   /* when we're here, we've deallocated any intervening contexts, and now
  1371.      we need to restore the state of the world as it was before we were called.
  1372.      Our caller has set the stack pointer to where we should place the
  1373.      return value, so all we need do is restore the interpreter's state and
  1374.      we're set. */
  1375.   /* ??? Geez, this feels clumsy.  We could have merged the "pop context"
  1376.      code below with the while loop above, using a do...while, but I wonder
  1377.      if, over the long haul, the code for popping the final context will
  1378.      be a special case and so will need separate code.  */
  1379.   oldContextOOP = thisContextOOP;
  1380.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1381.   thisContextOOP = thisContext->sender;
  1382.  
  1383.   if (!isBlockContext(oldContextOOP)) {
  1384.     if (!isNil(thisContext->hasBlock)) {
  1385.       /* mark it so block can't return from method */
  1386.       thisContext->sender = nilOOP;
  1387.     }
  1388.   }
  1389.  
  1390.   maybeMoveOOP(thisContextOOP);
  1391.   
  1392.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  1393.  
  1394.   methodContextOOP = getMethodContext(thisContextOOP);
  1395.   if (methodContextOOP != thisContextOOP) { /* if we're a block */
  1396.     maybeMoveOOP(methodContextOOP); /* validate containing method */
  1397.   }
  1398.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  1399.   thisMethod = methodContext->method;
  1400.   maybeMoveOOP(thisMethod);
  1401.   ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  1402.   sp = thisContext->contextStack + toInt(thisContext->spOffset);
  1403.  
  1404.   /* temporaries and self live in the method, not in the block */
  1405.   temporaries = methodContext->contextStack;
  1406.   self = methodContext->receiver;
  1407.   maybeMoveOOP(self);
  1408.  
  1409.   maybeMoveOOP(returnedValue);
  1410.  
  1411.   setStackTop(returnedValue);
  1412. }
  1413.  
  1414.  
  1415.  
  1416. /***********************************************************************
  1417.  *
  1418.  *    Simple Method Object Accessors
  1419.  *
  1420.  ***********************************************************************/
  1421.  
  1422. #ifdef ACCESSOR_DEBUGGING
  1423.  
  1424. static OOP receiverVariable(receiver, index)
  1425. OOP    receiver;
  1426. int    index;
  1427. {
  1428.   if (!inBounds(receiver, index)) {
  1429.     errorf("Index out of bounds %d", index);
  1430.   }
  1431.   return (oopToObj(receiver)->data[index]);
  1432. }
  1433.  
  1434. static OOP getStackReceiver(numArgs)
  1435. int    numArgs;
  1436. {
  1437.   /* this is correct: numArgs == 0 means that there's just the receiver
  1438.      on the stack, at 0.  numArgs = 1 means that at location 0 is the arg,
  1439.      location 1 is the receiver. */
  1440.   return (stackAt(numArgs));
  1441. }
  1442.  
  1443. static OOP methodTemporary(index)
  1444. int    index;
  1445. {
  1446.   return (temporaries[index]);
  1447. }
  1448.  
  1449. static OOP methodLiteral(methodOOP, index)
  1450. OOP    methodOOP;
  1451. int    index;
  1452. {
  1453.   Method    method = (Method)oopToObj(methodOOP);
  1454.  
  1455.   /* ### check for in bounds with index */
  1456.   return (method->literals[index]);
  1457. }
  1458.  
  1459. static OOP methodVariable(methodOOP, index)
  1460. OOP    methodOOP;
  1461. int    index;
  1462. {
  1463.   Method    method = (Method)oopToObj(methodOOP);
  1464.  
  1465.   return (associationValue(method->literals[index]));
  1466. }
  1467.  
  1468. static Byte *getMethodByteCodes(methodOOP)
  1469. OOP    methodOOP;
  1470. {
  1471.   Method    method;
  1472.  
  1473.   if (isNil(methodOOP)) {
  1474.     return (nil);
  1475.   }
  1476.  
  1477.   method = (Method)oopToObj(methodOOP);
  1478.  
  1479.   /* skip the header and the number of literals to find the start of the
  1480.      byte codes */
  1481.   return ((Byte *)&method->literals[method->header.numLiterals]);
  1482. }
  1483.  
  1484. static MethodHeader getMethodHeader(methodOOP)
  1485. OOP    methodOOP;
  1486. {
  1487.   Method    method;
  1488.  
  1489.   method = (Method)oopToObj(methodOOP);
  1490.   return (method->header);
  1491. }
  1492.  
  1493. /*
  1494.  *    static OOP getMethodClass(method)
  1495.  *
  1496.  * Description
  1497.  *
  1498.  *    This is called when a method contains a send to "super".  The compiler
  1499.  *    is supposed to notice a send to "super", and make sure that the last
  1500.  *    literal of a method is an association between the symbol for the
  1501.  *    class of the method and the class of the method itself.  This routine
  1502.  *    returns the class of the method itself using this association.
  1503.  *
  1504.  * Inputs
  1505.  *
  1506.  *    method: An OOP that represents a method.
  1507.  *
  1508.  * Outputs
  1509.  *
  1510.  *    An OOP for the class of the method.
  1511.  */
  1512. static OOP getMethodClass(methodOOP)
  1513. OOP    methodOOP;
  1514. {
  1515.   Method    method;
  1516.   OOP        associationOOP;
  1517.  
  1518.   method = (Method)oopToObj(methodOOP);
  1519.   associationOOP = method->literals[method->header.numLiterals - 1];
  1520.   return (associationValue(associationOOP));
  1521. }
  1522.  
  1523. /***********************************************************************
  1524.  *
  1525.  *    Simple Method Object Storing routines.
  1526.  *
  1527.  ***********************************************************************/
  1528.  
  1529.  
  1530. static void storeReceiverVariable(receiver, index, oop)
  1531. OOP    receiver, oop;
  1532. int    index;
  1533. {
  1534.   if (!inBounds(receiver, index)) {
  1535.     errorf("Index out of bounds %d", index);
  1536.   }
  1537.   prepareToStore(receiver, oop);
  1538.   oopToObj(receiver)->data[index] = oop;
  1539. }
  1540.  
  1541. static void storeMethodTemporary(index, oop)
  1542. int    index;
  1543. OOP    oop;
  1544. {
  1545.   prepareToStore(thisContextOOP, oop);
  1546.   temporaries[index] = oop;
  1547. }
  1548.  
  1549. static void storeMethodVariable(methodOOP, index, oop)
  1550. OOP    methodOOP, oop;
  1551. int    index;
  1552. {
  1553.   Method    method = (Method)oopToObj(methodOOP);
  1554.  
  1555.   setAssociationValue(method->literals[index], oop);
  1556. }
  1557.  
  1558. static void storeMethodLiteral(methodOOP, index, oop)
  1559. OOP    methodOOP, oop;
  1560. int    index;
  1561. {
  1562.   Method    method = (Method)oopToObj(methodOOP);
  1563.  
  1564.   prepareToStore(methodOOP, oop);
  1565.   method->literals[index] = oop;
  1566. }
  1567.  
  1568. static Boolean inBounds(oop, index)
  1569. OOP    oop;
  1570. int    index;
  1571. {
  1572.   Object    obj = oopToObj(oop);
  1573.  
  1574.   return (index >= 0 && index < numOOPs(obj));
  1575. }
  1576. #endif /* ACCESSOR_DEBUGGING */
  1577.  
  1578. MethodHeader getMethodHeaderExt(methodOOP)
  1579. OOP    methodOOP;
  1580. {
  1581.   return (getMethodHeader(methodOOP));
  1582. }
  1583.  
  1584. void storeMethodLiteralExt(methodOOP, index, oop)
  1585. OOP    methodOOP, oop;
  1586. int    index;
  1587. {
  1588.   storeMethodLiteral(methodOOP, index, oop);
  1589. }
  1590.  
  1591. /*
  1592.  *    void storeMethodLiteralNoGC(methodOOP, index, oop)
  1593.  *
  1594.  * Description
  1595.  *
  1596.  *    This routine exists primarily for the binary save/restore code.  Rather
  1597.  *    than adding a test of the garbage collector's state to a very busy
  1598.  *    routine, it's better to create a a clone that doesn't do the prepare to
  1599.  *    store.  If this routine were more complicated, it would make sense to
  1600.  *    do the test in storeMethodLiteral (ala instVarAtPut).
  1601.  *
  1602.  * Inputs
  1603.  *
  1604.  *    methodOOP: 
  1605.  *        A method OOP to set the literal of.
  1606.  *    index : the zero-based index of the literal to set
  1607.  *    oop   : the OOP to store into the method's literal table.
  1608.  *
  1609.  */
  1610. void storeMethodLiteralNoGC(methodOOP, index, oop)
  1611. OOP    methodOOP, oop;
  1612. int    index;
  1613. {
  1614.   Method    method = (Method)oopToObj(methodOOP);
  1615.  
  1616.   method->literals[index] = oop;
  1617. }
  1618.  
  1619. /*
  1620.  *    OOP methodLiteralExt(methodOOP, index)
  1621.  *
  1622.  * Description
  1623.  *
  1624.  *    External accessor routine.  Returns a literal from the given method.
  1625.  *
  1626.  * Inputs
  1627.  *
  1628.  *    methodOOP: 
  1629.  *        A CompiledMethod OOP.
  1630.  *    index : An index into the literals of the method.
  1631.  *
  1632.  * Outputs
  1633.  *
  1634.  *    The literal at index in the CompiledMethod.
  1635.  */
  1636. OOP methodLiteralExt(methodOOP, index)
  1637. OOP    methodOOP;
  1638. int    index;
  1639. {
  1640.   return (methodLiteral(methodOOP, index));
  1641. }
  1642.  
  1643. /*
  1644.  *    Boolean equal(oop1, oop2)
  1645.  *
  1646.  * Description
  1647.  *
  1648.  *    Internal definition of equality.  Returns true if "oop1" and "oop2" are
  1649.  *    the same object, false if they are not, and false and an error if they
  1650.  *    are not the same and not both Symbols.
  1651.  *
  1652.  * Inputs
  1653.  *
  1654.  *    oop1  : An OOP to be compared, typically a Symbol.
  1655.  *    oop2  : An OOP to be compared, typically a Symbol.
  1656.  *
  1657.  * Outputs
  1658.  *
  1659.  *    True if the two objects are the same object, false if not, and an error
  1660.  *    message if they are not the same and not both symbols.
  1661.  */
  1662. Boolean equal(oop1, oop2)
  1663. OOP    oop1, oop2;
  1664. {
  1665.   if (oop1 == oop2) {
  1666.     /* no brain case (ha ha ha) */
  1667.     return (true);
  1668.   }
  1669.  
  1670.   if (isClass(oop1, symbolClass) && isClass(oop2, symbolClass)) {
  1671.     return (false);
  1672.   }
  1673.  
  1674.   errorf("Internal #= called with invalid object types\n");
  1675.   return (false);
  1676. }
  1677.  
  1678. /*
  1679.  *    long hash(oop)
  1680.  *
  1681.  * Description
  1682.  *
  1683.  *    Internal hash function.  Currently defined only for symbols, but may be
  1684.  *    extended as needed for other objects.  The definition of the hash
  1685.  *    function used here must be the same as that defined in Smalltalk
  1686.  *    methods.
  1687.  *
  1688.  * Inputs
  1689.  *
  1690.  *    oop   : An OOP to be hashed.
  1691.  *
  1692.  * Outputs
  1693.  *
  1694.  *    Hash value of the OOP, or 0 and an error message if the OOP does not
  1695.  *    have a defined has value (that this routine knows how to compute).
  1696.  */
  1697. long hash(oop)
  1698. OOP    oop;
  1699. {
  1700.   if (!isInt(oop) && oopClass(oop) == symbolClass) {
  1701.     return (oopIndex(oop));
  1702.   }
  1703.  
  1704.   errorf("Internal #hash called with invalid object type\n");
  1705.   return (0);
  1706. }
  1707.  
  1708. /*
  1709.  *    static Boolean executePrimitiveOperation(primitive, numArgs, methodOOP)
  1710.  *
  1711.  * Description
  1712.  *
  1713.  *    This routine provides the definitions of all of the primitive methods
  1714.  *    in the GNU Smalltalk system.  It normally removes the arguments to the
  1715.  *    primitive methods from the stack, but if the primitive fails, the
  1716.  *    arguments are put back onto the stack and this routine returns false,
  1717.  *    indicating failure to invoke the primitive.
  1718.  *
  1719.  * Inputs
  1720.  *
  1721.  *    primitive: 
  1722.  *        A C int that indicates the number of the primitive to invoke.
  1723.  *        Must be > 0.
  1724.  *    numArgs: 
  1725.  *        The number of arguments that the primitive has.
  1726.  *    methodOOP: 
  1727.  *        The OOP for the currently executing method.  This allows
  1728.  *        primitives to poke around in the method itself, to get at
  1729.  *        pieces that they need.  Normally, this is only used by the C
  1730.  *        callout routine to get at the compiled-in descriptor for the
  1731.  *        called C function.
  1732.  *
  1733.  * Outputs
  1734.  *
  1735.  *    True if the execution of the primitive operation succeeded, false if it
  1736.  *    failed for some reason.
  1737.  */
  1738. static Boolean executePrimitiveOperation(primitive, numArgs, methodOOP)
  1739. int    primitive, numArgs;
  1740. OOP    methodOOP;
  1741. {
  1742.   Boolean    failed, atEof;
  1743.   OOP        oop, oop1, oop2, oop3, oop4, oopVec[4], classOOP, fileOOP,
  1744.         blockContextOOP;
  1745.   long        arg1, arg2, arg3;
  1746.   double    farg1, farg2, fdummy;
  1747.   int        i, ch;
  1748.   BlockContext    blockContext;
  1749.   Byte        *fileName, *fileMode;
  1750.   FILE        *file;
  1751.   FileStream    fileStream;
  1752.   Semaphore    sem;
  1753. #if !defined(USG)
  1754.   struct timeval tv;
  1755. #else
  1756.   time_t tv;
  1757. #endif
  1758.   struct stat    statBuf;
  1759. #ifdef LOCAL_REGS
  1760.   register OOP    *sp;
  1761. #endif /* LOCAL_REGS */
  1762.  
  1763.   importSP();
  1764.  
  1765. #ifdef countingByteCodes
  1766.   primitives[primitive]++;
  1767. #endif
  1768.  
  1769.   failed = true;
  1770.   switch (primitive) {
  1771.   case  1: case  2: case  3: case  4:
  1772.   case  5: case  6: case  7: case  8:
  1773.   case  9: case 10: case 11: case 12:
  1774.   case 13: case 14: case 15: case 16:
  1775.   case 17:
  1776.     oop2 = popOOP();
  1777.     oop1 = popOOP();
  1778.     if (isInt(oop1) && isInt(oop2)) {
  1779.       failed = false;
  1780.       arg1 = toInt(oop1);
  1781.       arg2 = toInt(oop2);
  1782.       /* ??? make this faster by not pushing and popping */
  1783.  
  1784.       switch(primitive) {
  1785.       case 1:    pushInt(arg1 + arg2);        break;
  1786.       case 2:    pushInt(arg1 - arg2);        break;
  1787.       case 3:    pushBoolean(arg1 < arg2);    break;
  1788.       case 4:    pushBoolean(arg1 > arg2);    break;
  1789.       case 5:    pushBoolean(arg1 <= arg2);    break;
  1790.       case 6:    pushBoolean(arg1 >= arg2);    break;
  1791.       case 7:    pushBoolean(arg1 == arg2);    break;
  1792.       case 8:    pushBoolean(arg1 != arg2);    break;
  1793.       case 9:    pushInt(arg1 * arg2);        break; /* ### overflow? */
  1794.       case 10:
  1795.     if (arg2 != 0 /*&& (arg1 % arg2) == 0*/) { /* ### fix this when coercing goes in */
  1796.       pushInt(arg1 / arg2);
  1797.     } else {
  1798.       failed = true;
  1799.     }
  1800.     break;
  1801.       case 11:
  1802.     if (arg2 != 0) {
  1803.       if ((arg1 ^ arg2) < 0) {
  1804.         /* ??? help...is there a better way to do this? */
  1805.         pushInt(arg1 - ((arg1 - (arg2-1)) / arg2) * arg2);
  1806.       } else {
  1807.         pushInt(arg1 % arg2);
  1808.       }
  1809.     } else {
  1810.       failed = true;
  1811.     }
  1812.     break;
  1813.       case 12:
  1814.     if (arg2 != 0) {
  1815.       if ((arg1 ^ arg2) < 0) { /* differing signs => negative result */
  1816.         pushInt((arg1 - (arg2-1)) / arg2);
  1817.       } else {
  1818.         pushInt(arg1 / arg2);
  1819.       }
  1820.     } else {
  1821.       failed = true;
  1822.     }
  1823.     break;
  1824.       case 13:
  1825.     if (arg2 != 0) {
  1826.       pushInt(arg1 / arg2);
  1827.     } else {
  1828.       failed = true;
  1829.     }
  1830.     break;
  1831.       case 14:    pushInt(arg1 & arg2);          break;
  1832.       case 15:    pushInt(arg1 | arg2);        break;
  1833.       case 16:    pushInt(arg1 ^ arg2);        break;
  1834.       case 17:
  1835.     /* ??? check for overflow */
  1836.     if (arg2 >= 0) {
  1837.       pushInt(arg1 << arg2);
  1838.     } else {
  1839.       pushInt(arg1 >> -arg2);
  1840.     }
  1841.     break;
  1842.       }
  1843.     }
  1844.  
  1845.     if (failed) {
  1846.       unPop(2);
  1847.     }
  1848.     break;
  1849.  
  1850.   case 40:
  1851.     oop1 = popOOP();
  1852.     if (isInt(oop1)) {
  1853.       pushOOP(floatNew((double)toInt(oop1)));
  1854.       failed = false;
  1855.     }
  1856.  
  1857.     if (failed) {
  1858.       unPop(1);
  1859.     }
  1860.     break;
  1861.  
  1862.   case 41: case 42: case 43: case 44:
  1863.   case 45: case 46: case 47: case 48:
  1864.   case 49: case 50:
  1865.     oop2 = popOOP();
  1866.     oop1 = popOOP();
  1867.     if (isClass(oop1, floatClass) && isClass(oop2, floatClass)) {
  1868.       failed = false;
  1869.       farg1 = floatOOPValue(oop1);
  1870.       farg2 = floatOOPValue(oop2);
  1871.       switch (primitive) {
  1872.       case 41:    pushOOP(floatNew(farg1 + farg2));    break;
  1873.       case 42:    pushOOP(floatNew(farg1 - farg2));    break;
  1874.       case 43:    pushBoolean(farg1 < farg2);         break;
  1875.       case 44:    pushBoolean(farg1 > farg2);        break;
  1876.       case 45:    pushBoolean(farg1 <= farg2);        break;
  1877.       case 46:    pushBoolean(farg1 >= farg2);        break;
  1878.       case 47:    pushBoolean(farg1 == farg2);        break;
  1879.       case 48:    pushBoolean(farg1 != farg2);        break;
  1880.       case 49:    pushOOP(floatNew(farg1 * farg2));    break;
  1881.       case 50:
  1882.     if (farg2 != 0.0) {
  1883.       pushOOP(floatNew(farg1 / farg2));
  1884.     } else {
  1885.       failed = true;
  1886.     }
  1887.     break;
  1888.       }
  1889.     }
  1890.  
  1891.     if (failed) {
  1892.       unPop(2);
  1893.     }
  1894.     break;
  1895.  
  1896.   case 51:            /* Float truncated */
  1897.     oop1 = popOOP();
  1898.     if (isClass(oop1, floatClass)) {
  1899.       failed = false;
  1900.       pushInt((long)floatOOPValue(oop1));
  1901.     } else {
  1902.       unPop(1);
  1903.     }
  1904.     break;
  1905.  
  1906.   case 52:            /* Float fractionPart */
  1907.     oop1 = popOOP();
  1908.     if (isClass(oop1, floatClass)) {
  1909.       failed = false;
  1910.       farg1 = floatOOPValue(oop1);
  1911.       if (farg1 < 0.0) {
  1912.     farg1 = -farg1;
  1913.       }
  1914.       pushOOP(floatNew(modf(farg1, &fdummy)));
  1915.     } else {
  1916.       unPop(1);
  1917.     }
  1918.     break;
  1919.  
  1920.   case 53:            /* Float exponent */
  1921.     oop1 = popOOP();
  1922.     if (isClass(oop1, floatClass)) {
  1923.       failed = false;
  1924.       farg1 = floatOOPValue(oop1);
  1925.       if (farg1 == 0.0) {
  1926.     arg1 = 1;
  1927.       } else {
  1928.     frexp(floatOOPValue(oop1), (int *)&arg1);
  1929.       }
  1930.       pushInt(arg1-1);
  1931.     } else {
  1932.       unPop(1);
  1933.     }
  1934.     break;
  1935.  
  1936.   case 54:            /* Float timesTwoPower: */
  1937.     oop2 = popOOP();
  1938.     oop1 = popOOP();
  1939.     if (isClass(oop1, floatClass) && isInt(oop2)) {
  1940.       failed = false;
  1941.       farg1 = floatOOPValue(oop1);
  1942.       arg2 = toInt(oop2);
  1943. #ifdef SUNOS40
  1944.       pushOOP(floatNew(scalbn(farg1, arg2)));
  1945. #else
  1946.       pushOOP(floatNew(ldexp(farg1, arg2)));
  1947. #endif
  1948.     } else {
  1949.       unPop(2);
  1950.     }
  1951.     break;
  1952.  
  1953.   case 60:            /* Object at:, Object basicAt: */
  1954.     oop2 = popOOP();
  1955.     oop1 = popOOP();
  1956.     if (isInt(oop2)) {
  1957.       arg2 = toInt(oop2);
  1958.       if (checkIndexableBoundsOf(oop1, arg2)) {
  1959.     failed = false;
  1960.     pushOOP(indexOOP(oop1, arg2));
  1961.       }
  1962.     }
  1963.  
  1964.     if (failed) {
  1965.       unPop(2);
  1966.     }
  1967.     break;
  1968.  
  1969.   case 61:            /* Object at:put:, Object basicAt:put: */
  1970.     oop3 = popOOP();
  1971.     oop2 = popOOP();
  1972.     oop1 = popOOP();
  1973.     if (isInt(oop2)) {
  1974.       arg2 = toInt(oop2);
  1975.       if (checkIndexableBoundsOf(oop1, arg2)) {
  1976.     if (indexOOPPut(oop1, arg2, oop3)) {
  1977.       failed = false;
  1978.       pushOOP(oop3);
  1979.     }
  1980.       }
  1981.     }
  1982.  
  1983.     if (failed) {
  1984.       unPop(3);
  1985.     }
  1986.     break;
  1987.  
  1988.   case 62:            /* Object basicSize; Object size; String size;
  1989.                    ArrayedCollection size */
  1990.     oop1 = popOOP();
  1991.     pushOOP(fromInt(numIndexableFields(oop1)));
  1992.     failed = false;
  1993.     break;
  1994.  
  1995.   case 63:            /* String at:; String basicAt: */
  1996.     oop2 = popOOP();
  1997.     oop1 = popOOP();
  1998.     if (isInt(oop2)) {
  1999.       arg2 = toInt(oop2);
  2000.       if (checkIndexableBoundsOf(oop1, arg2)) {
  2001.     pushOOP(indexStringOOP(oop1, arg2));
  2002.     failed = false;
  2003.       }
  2004.     }
  2005.  
  2006.     if (failed) {
  2007.       unPop(2);
  2008.     }
  2009.     break;
  2010.  
  2011.   case 64:            /* String basicAt:put:; String at:put: */
  2012.     oop3 = popOOP();
  2013.     oop2 = popOOP();
  2014.     oop1 = popOOP();
  2015.  
  2016.     if (isInt(oop2) && isClass(oop3, charClass)) {
  2017.       arg2 = toInt(oop2);
  2018.       if (checkIndexableBoundsOf(oop1, arg2)) {
  2019.     indexStringOOPPut(oop1, arg2, oop3);
  2020.     failed = false;
  2021.     pushOOP(oop3);
  2022.       }
  2023.     }
  2024.  
  2025.     if (failed) {
  2026.       unPop(3);
  2027.     }
  2028.     break;
  2029.  
  2030.   case 68:            /* CompiledMethod objectAt: */
  2031.     oop2 = popOOP();
  2032.     oop1 = popOOP();
  2033.     if (isClass(oop1, compiledMethodClass) && isInt(oop2)) {
  2034.       arg2 = toInt(oop2);
  2035.       if (validMethodIndex(oop1, arg2)) {
  2036.     failed = false;
  2037.     pushOOP(compiledMethodAt(oop1, arg2));
  2038.       }
  2039.     }
  2040.  
  2041.     if (failed) {
  2042.       unPop(2);
  2043.     }
  2044.     break;
  2045.  
  2046.   case 69:            /* CompiledMethod objectAt:put: */
  2047.     oop3 = popOOP();
  2048.     oop2 = popOOP();
  2049.     oop1 = stackTop();
  2050.     if (isClass(oop1, compiledMethodClass) && isInt(oop2)) {
  2051.       arg2 = toInt(oop2);
  2052.       if (validMethodIndex(oop1, arg2)) {
  2053.     failed = false;
  2054.     compiledMethodAtPut(oop1, arg2, oop3);
  2055.       }
  2056.     }
  2057.  
  2058.     if (failed) {
  2059.       unPop(2);
  2060.     }
  2061.     break;
  2062.  
  2063.   case 70:            /* Behavior basicNew; Behavior new;
  2064.                    Interval class new */
  2065.     oop1 = popOOP();
  2066.     if (isOOP(oop1)) {
  2067.       if (!isIndexable(oop1)) {
  2068.     pushOOP(allocOOP(instantiate(oop1)));
  2069.     failed = false;
  2070.       }
  2071.     }
  2072.  
  2073.     if (failed) {
  2074.       unPop(1);
  2075.     }
  2076.     break;
  2077.  
  2078.  
  2079.   case 71:            /* Behavior new:; Behavior basicNew: */
  2080.     oop2 = popOOP();
  2081.     oop1 = popOOP();
  2082.     if (isOOP(oop1) && isInt(oop2)) {
  2083.       if (isIndexable(oop1)) {
  2084.     arg2 = toInt(oop2);
  2085.     pushOOP(instantiateOOPWith(oop1, arg2));
  2086.     failed = false;
  2087.       }
  2088.     }
  2089.  
  2090.     if (failed) {
  2091.       unPop(2);
  2092.     }
  2093.     break;
  2094.  
  2095.   case 72:            /* Object become: */
  2096.     oop2 = popOOP();
  2097.     oop1 = popOOP();
  2098.     if (isOOP(oop1) && isOOP(oop2)) {
  2099.       swapObjects(oop1, oop2);
  2100.       pushOOP(oop1);
  2101.       failed = false;
  2102.     }
  2103.  
  2104.     if (failed) {
  2105.       unPop(2);
  2106.     }
  2107.     break;
  2108.  
  2109.   case 73:            /* Object instVarAt: */
  2110.     oop2 = popOOP();
  2111.     oop1 = popOOP();
  2112.     if (isInt(oop2)) {
  2113.       arg2 = toInt(oop2);
  2114.       if (checkBoundsOf(oop1, arg2)) {
  2115.     failed = false;
  2116.     pushOOP(instVarAt(oop1, arg2));
  2117.       }
  2118.     }
  2119.  
  2120.     if (failed) {
  2121.       unPop(2);
  2122.     }
  2123.     break;
  2124.  
  2125.   case 74:            /* Object instVarAt:put: */
  2126.     oop3 = popOOP();
  2127.     oop2 = popOOP();
  2128.     oop1 = popOOP();
  2129.     if (isInt(oop2)) {
  2130.       arg2 = toInt(oop2);
  2131.       if (checkBoundsOf(oop1, arg2)) {
  2132.     if (instVarAtPut(oop1, arg2, oop3)) {
  2133.       failed = false;
  2134.     }
  2135.       }
  2136.     }
  2137.  
  2138.     if (failed) {
  2139.       unPop(2);
  2140.     }
  2141.     break;
  2142.  
  2143.   case 75:            /* Object asOop; Object hash; Symbol hash */
  2144.     oop1 = popOOP();
  2145.     if (isOOP(oop1)) {
  2146.       failed = false;
  2147.       pushInt(oopIndex(oop1));
  2148.     }
  2149.  
  2150.     if (failed) {
  2151.       unPop(1);
  2152.     }
  2153.     break;
  2154.  
  2155.   case 76:            /* SmallInteger asObject;
  2156.                    SmallInteger asObjectNoFail */
  2157.     oop1 = popOOP();
  2158.     if (isInt(oop1)) {
  2159.       arg1 = toInt(oop1);
  2160.       if (oopIndexValid(arg1)) {
  2161.     failed = false;
  2162.     pushOOP(oopAt(arg1-1));
  2163.       }
  2164.     }
  2165.  
  2166.     if (failed) {
  2167.       unPop(1);
  2168.     }
  2169.     break;
  2170.  
  2171.   case 77:            /* Behavior someInstance */
  2172.     oop1 = popOOP(); 
  2173.     for (oop = oopTable; oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]; oop++) {
  2174.       if (oopValid(oop) && oop1 == oopClass(oop)) {
  2175.     pushOOP(oop);
  2176.     failed = false;
  2177.     break;
  2178.       }
  2179.     }
  2180.  
  2181.     if (failed) {
  2182.       unPop(1);
  2183.     }
  2184.     break;
  2185.  
  2186.   case 78:            /* Object nextInstance */
  2187.     oop1 = popOOP();
  2188.     if (!isInt(oop1)) {
  2189.       classOOP = oopClass(oop1);
  2190.       for (oop = oop1 + 1; oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]; oop++) {
  2191.     if (oopValid(oop) && classOOP == oopClass(oop)) {
  2192.       failed = false;
  2193.       pushOOP(oop);
  2194.       break;
  2195.     }
  2196.       }
  2197.     }
  2198.  
  2199.     if (failed) {
  2200.       unPop(1);
  2201.     }
  2202.     break;
  2203.  
  2204.   case 79:            /* CompiledMethod class newMethod:header: */
  2205.     oop3 = popOOP();
  2206.     oop2 = popOOP();
  2207.     oop1 = popOOP();
  2208.     if (isInt(oop3) && isInt(oop2)) {
  2209.       failed = false;
  2210.       arg3 = toInt(oop3);
  2211.       arg2 = toInt(oop2);
  2212.       pushOOP(methodNewOOP(arg2, arg3));
  2213.     }
  2214.       
  2215.     if (failed) {
  2216.       unPop(3);
  2217.     }
  2218.     break;
  2219.  
  2220.   case 80:            /* ContextPart blockCopy: */
  2221.     oop2 = popOOP();
  2222.     oop1 = popOOP();
  2223.     if (isInt(oop2)) {
  2224.       failed = false;
  2225.       arg2 = toInt(oop2);
  2226.       blockContextOOP = allocBlockContext();
  2227.       blockContext = (BlockContext)oopToObj(blockContextOOP);
  2228.       blockContext->home = getMethodContext(oop1);
  2229.       maybeMoveOOP(blockContext->home);
  2230.       blockContext->numArgs = oop2;
  2231.       methodHasBlockContext(blockContext->home);
  2232.       /* the +2 here is to skip over the jump byte codes that follow the
  2233.      invocation of blockCopy, so that the ipIndex points to the first
  2234.      byte code of the block. */
  2235.       blockContext->initialIP = fromInt(relativeByteIndex(ip, thisMethod) + 2);
  2236.       if (oopClass(blockContext->home) != methodContextClass) {
  2237.     errorf("Block's home is not a MethodContext!!!\n");
  2238.       }
  2239.       pushOOP(blockContextOOP);
  2240.     }
  2241.  
  2242.     if (failed) {
  2243.       unPop(2);
  2244.     }
  2245.     break;
  2246.  
  2247.   case 81:            /* BlockContext value
  2248.                    BlockContext value:
  2249.                    BlockContext value:value:
  2250.                    BlockContext value:value:value: */
  2251.     exportSP();
  2252.     sendBlockValue(numArgs);    /* ### check number of args for agreement! */
  2253.     importSP();
  2254.  
  2255.     failed = false;
  2256.     break;
  2257.  
  2258.   case 82:            /* BlockContext valueWithArguments: */
  2259.     oop2 = popOOP();
  2260.     oop1 = stackTop();
  2261.     if (isClass(oop2, arrayClass)) {
  2262.       failed = false;
  2263.       numArgs = numIndexableFields(oop2);
  2264.       for (i = 1; i <= numArgs; i++) {
  2265.     pushOOP(arrayAt(oop2, i));
  2266.       }
  2267.       exportSP();
  2268.       sendBlockValue(numArgs);
  2269.       importSP();
  2270.     }
  2271.  
  2272.     if (failed) {
  2273.       unPop(1);
  2274.     }
  2275.     break;
  2276.  
  2277.   case 83:            /* Object perform:
  2278.                    Object perform:with:
  2279.                    Object perform:with:with:
  2280.                    Object perform:with:with:with: */
  2281.     failed = false;
  2282.     /* pop off the arguments (if any) */
  2283.     for (i = 0; i < numArgs - 1; i++) {
  2284.       oopVec[i] = popOOP();
  2285.     }
  2286.     oop1 = popOOP();        /* the selector */
  2287.     /* push the args back onto the stack */
  2288.     for (; --i >= 0; ) {
  2289.       pushOOP(oopVec[i]);
  2290.     }
  2291.     exportSP();
  2292.     sendMessage(oop1, numArgs - 1, false);
  2293.     importSP();
  2294.     break;
  2295.  
  2296.   case 84:            /* Object perform:withArguments: */
  2297.  
  2298.     oop2 = popOOP();
  2299.     oop1 = popOOP();
  2300.     if (isClass(oop2, arrayClass)) {
  2301.       failed = false;
  2302.       numArgs = numIndexableFields(oop2);
  2303.       for (i = 1; i <= numArgs; i++) {
  2304.     pushOOP(arrayAt(oop2, i));
  2305.       }
  2306.       exportSP();
  2307.       sendMessage(oop1, numArgs, false);
  2308.       importSP();
  2309.     }
  2310.  
  2311.     if (failed) {
  2312.       unPop(2);
  2313.     }
  2314.     break;
  2315.  
  2316.   case 85:            /* Semaphore signal */
  2317.     oop1 = stackTop();
  2318.     if (isClass(oop1, semaphoreClass)) {
  2319.       failed = false;
  2320.       syncSignal(oop1);
  2321.     }
  2322.  
  2323.     break;
  2324.  
  2325.   case 86:            /* Semaphore wait */
  2326.     oop1 = stackTop();
  2327.     if (isClass(oop1, semaphoreClass)) {
  2328.       failed = false;
  2329.       sem = (Semaphore)oopToObj(oop1);
  2330.       if (toInt(sem->signals) > 0) {    /* no waiting here */
  2331.     sem->signals = decrInt(sem->signals);
  2332.       } else {            /* have to suspend */
  2333.     addLastLink(oop1, getActiveProcess());
  2334.     suspendActiveProcess();
  2335.       }
  2336.     }
  2337.  
  2338.     break;
  2339.  
  2340.   case 87:            /* Process resume */
  2341.     resumeProcess(stackTop());
  2342.     failed = false;
  2343.     break;
  2344.  
  2345.   case 88:            /* Process suspend */
  2346.     oop1 = popOOP();
  2347.     if (oop1 == getActiveProcess()) {
  2348.       failed = false;
  2349.       pushOOP(nilOOP);        /* this is our return value */
  2350.       suspendActiveProcess();
  2351.     }
  2352.  
  2353.     if (failed) {
  2354.       unPop(1);
  2355.     }
  2356.     break;
  2357.  
  2358.  
  2359.   case 98:            /* Time class secondClock
  2360.                  *  -- note: this primitive has different
  2361.                  *     semantics from those defined in the
  2362.                  *     book.  This primitive returns the
  2363.                  *     seconds since Jan 1, 1970 00:00:00
  2364.                  *     instead of Jan 1,1901.
  2365.                  */
  2366.     popOOP();
  2367.     failed = false;
  2368. #if !defined(USG)
  2369.     gettimeofday(&tv, nil);
  2370.     pushInt(tv.tv_sec);
  2371. #else
  2372.     (void) time(&tv);
  2373.     pushInt(tv);
  2374. #endif
  2375.     break;
  2376.  
  2377.   case 99:            /* Time class millisecondClock
  2378.                  * -- Note: the semantics of this primitive
  2379.                  *    are different than those described in
  2380.                  *    the book.  This primitive returns the
  2381.                  *    number of milliseconds since midnight
  2382.                  *    today. */
  2383.     popOOP();
  2384.     failed = false;
  2385. #if !defined(USG)
  2386.     gettimeofday(&tv, nil);
  2387.     pushInt((tv.tv_sec % (24*60*60)) * 1000 + tv.tv_usec / 1000);
  2388. #else
  2389.     (void) time(&tv);
  2390.     pushInt((tv % (24*60*60)) * 1000);
  2391. #endif
  2392.     break;
  2393.  
  2394.   case 110:            /* Object ==, Character = */
  2395.     oop2 = popOOP();
  2396.     oop1 = popOOP();
  2397.     pushBoolean(oop1 == oop2);
  2398.     failed = false;
  2399.     break;
  2400.  
  2401.   case 111:            /* Object class */
  2402.     oop1 = popOOP();
  2403.     /* ??? is this called with ints? */
  2404.     if (isInt(oop1)) {
  2405.       pushOOP(integerClass);
  2406.     } else {
  2407.       pushOOP(oopClass(oop1));
  2408.     }
  2409.     failed = false;
  2410.     break;
  2411.  
  2412.   case 113:            /* quitPrimitive */
  2413.     exit(0);
  2414.     break;
  2415.  
  2416.   case 128:            /* Dictionary at: */
  2417.     oop2 = popOOP();
  2418.     oop1 = popOOP();
  2419.     failed = false;
  2420.     pushOOP(dictionaryAt(oop1, oop2));
  2421.     break;
  2422.  
  2423.   case 129:            /* Dictionary at: put: */
  2424.     oop3 = popOOP();
  2425.     oop2 = popOOP();
  2426.     oop1 = popOOP();
  2427.     failed = false;
  2428.     dictionaryAtPut(oop1, oop2, oop3);
  2429.     pushOOP(oop3);
  2430.     break;
  2431.  
  2432.   case 130:            /* doesNotUnderstand: message */
  2433.     oop2 = popOOP();
  2434.     oop1 = popOOP();
  2435.     printObject(oop1);
  2436.     printf(" did not understand selector '");
  2437.     printSymbol(messageSelector(oop2));
  2438.     printf("'\n\n");
  2439.     showBacktrace();
  2440.     stopExecuting(0);
  2441.     failed = false;
  2442.     break;
  2443.  
  2444.   case 131:            /* error: message */
  2445.     oop2 = popOOP();        /* error string */
  2446.     oop1 = stackTop();        /* the receiver */
  2447.     printObject(oop1);
  2448.     printf(" error: ");
  2449.     printString(oop2);
  2450.     printf("\n\n");
  2451.     showBacktrace();
  2452.     stopExecuting(0);
  2453.     failed = false;
  2454.     break;
  2455.     
  2456.   case 132:            /* Character class value: */
  2457.     oop2 = popOOP();
  2458.     oop1 = popOOP();
  2459.     if (isInt(oop2)) {
  2460.       arg2 = toInt(oop2);
  2461.       if (arg2 >= 0 && arg2 <= 255) {
  2462.     failed = false;
  2463.     pushOOP(charOOPAt(arg2));
  2464.       }
  2465.     }
  2466.  
  2467.     if (failed) {
  2468.       unPop(2);
  2469.     }
  2470.     break;
  2471.  
  2472.   case 133:            /* Character asciiValue */
  2473.     oop1 = popOOP();
  2474.     pushOOP(fromInt(charOOPValue(oop1)));
  2475.     failed = false;
  2476.     break;
  2477.  
  2478.   case 134:            /* Symbol class intern: aString */
  2479.     oop2 = popOOP();
  2480.     oop1 = popOOP();
  2481.     if (isClass(oop2, stringClass)) {
  2482.       failed = false;
  2483.       pushOOP(internStringOOP(oop2));
  2484.     }
  2485.     if (failed) {
  2486.       unPop(2);
  2487.     }
  2488.     break;
  2489.  
  2490.   case 135:            /* Dictionary new */
  2491.     popOOP();            /* ignore receiver */
  2492.     pushOOP(dictionaryNew());
  2493.     failed = false;
  2494.     break;
  2495.  
  2496.   case 136:            /* ByteMemory at: */
  2497.     oop2 = popOOP();
  2498.     oop1 = popOOP();
  2499.     if (isInt(oop2)) {
  2500.       failed = false;
  2501.       arg2 = toInt(oop2);
  2502.       pushInt(*(Byte *)arg2);
  2503.     }
  2504.  
  2505.     if (failed) {
  2506.       unPop(2);
  2507.     }
  2508.     break;
  2509.     
  2510.   case 137:            /* ByteMemory at:put: */
  2511.     oop3 = popOOP();
  2512.     oop2 = popOOP();
  2513.     if (isInt(oop2) && isInt(oop3)) {
  2514.       arg1 = toInt(oop2);
  2515.       arg2 = toInt(oop3);
  2516.       if (arg2 >= 0 && arg2 <= 255) {
  2517.     failed = false;
  2518.     *(Byte *)arg1 = arg2;
  2519.       }
  2520.     }
  2521.  
  2522.     if (failed) {
  2523.       unPop(2);
  2524.     }
  2525.     break;
  2526.     
  2527.   case 138:            /* Memory addressOfOOP: oop */
  2528.     oop2 = popOOP();
  2529.     oop1 = popOOP();
  2530.     if (!isInt(oop2)) {
  2531.       failed = false;
  2532.       pushInt((long)oop2);
  2533.     }
  2534.  
  2535.     if (failed) {
  2536.       unPop(2);
  2537.     }
  2538.     break;
  2539.  
  2540.   case 139:            /* Memory addressOf: oop */
  2541.     oop2 = popOOP();
  2542.     oop1 = popOOP();
  2543.     if (!isInt(oop2)) {
  2544.       failed = false;
  2545.       pushInt((long)oopToObj(oop2));
  2546.     }
  2547.  
  2548.     if (failed) {
  2549.       unPop(2);
  2550.     }
  2551.     break;
  2552.  
  2553.   case 140:            /* SystemDictionary backtrace */
  2554.     showBacktrace();
  2555.     failed = false;
  2556.     break;
  2557.  
  2558.  
  2559.   case 141:            /* SystemDictionary executionTrace: aBoolean */
  2560.     oop1 = popOOP();
  2561.     if (oop1 == trueOOP) {
  2562.       executionTracing = true;
  2563.     } else {
  2564.       executionTracing = false;
  2565.     }
  2566.     exceptFlag = true;
  2567.     failed = false;
  2568.     break;
  2569.  
  2570.   case 142:            /* SystemDictionary declarationTrace: aBoolean */
  2571.     oop1 = popOOP();
  2572.     if (oop1 == trueOOP) {
  2573.       declareTracing = true;
  2574.     } else {
  2575.       declareTracing = false;
  2576.     }
  2577.     failed = false;
  2578.     break;
  2579.  
  2580.   case 143:            /* ClassDescription comment: aString */
  2581.     oop2 = popOOP();
  2582.     oop1 = stackTop();
  2583.     setComment(oop1, oop2);
  2584.     failed = false;
  2585.     break;
  2586.  
  2587.   case 150:            /* methodsFor: category */
  2588.     setCompilationCategory(popOOP());
  2589.     setCompilationClass(stackTop());
  2590.     failed = false;
  2591.     break;
  2592.  
  2593.  
  2594.   case 160:            /* exp */
  2595.   case 161:            /* ln */
  2596.     oop1 = popOOP();
  2597.     if (isClass(oop1, floatClass)) {
  2598.       failed = false;
  2599.       farg1 = floatOOPValue(oop1);
  2600.       switch (primitive) {
  2601.       case 160: pushOOP(floatNew(exp(farg1)));    break;
  2602.       case 161: pushOOP(floatNew(log(farg1)));    break;
  2603.       }
  2604.     }
  2605.     if (failed) {
  2606.       unPop(1);
  2607.     }
  2608.     break;
  2609.  
  2610.   /* case 162:            /* log: aNumber -- base aNumber log */
  2611.   /* case 163:            /* floorLog: radix -- integer floor operation */
  2612.  
  2613.   case 164:            /* raisedTo: aNumber -- receiver ** aNumber */
  2614.     oop2 = popOOP();
  2615.     oop1 = popOOP();
  2616.     if (isClass(oop1, floatClass) && isClass(oop2, floatClass)) {
  2617.       failed = false;
  2618.       farg1 = floatOOPValue(oop1);
  2619.       farg2 = floatOOPValue(oop2);
  2620.       pushOOP(floatNew(pow(farg1, farg2)));
  2621.     }
  2622.     if (failed) {
  2623.       unPop(2);
  2624.     }
  2625.     break;
  2626.  
  2627.  
  2628.   /* >>>>>> HOLE 165 <<<<<< */
  2629.  
  2630.   case 166:            /* sqrt -- floating result */
  2631.     oop1 = popOOP();
  2632.     if (isClass(oop1, floatClass)) {
  2633.       failed = false;
  2634.       farg1 = floatOOPValue(oop1);
  2635.       pushOOP(floatNew(sqrt(farg1)));
  2636.     }
  2637.     if (failed) {
  2638.       unPop(1);
  2639.     }
  2640.     break;
  2641.  
  2642.   /* >>>>>> 167: HOLE <<<<<< */
  2643.  
  2644.   case 168:            /* ceiling */
  2645.   case 169:            /* floor */
  2646.     oop1 = popOOP();
  2647.     if (isClass(oop1, floatClass)) {
  2648.       failed = false;
  2649.       farg1 = floatOOPValue(oop1);
  2650.       switch (primitive) {
  2651.       case 168: pushInt((long)ceil(farg1));    break;
  2652.       case 169: pushInt((long)floor(farg1));    break;
  2653.       }
  2654.     }
  2655.     if (failed) {
  2656.       unPop(1);
  2657.     }
  2658.     break;
  2659.  
  2660.  
  2661.   case 171:            /* truncateTo: aNumber the next multiple of aNumber nearest the receiver towards zero */
  2662.   case 172:            /* rounded -- integer nearest the receiver */
  2663.   case 173:            /* roundTo: aNumber -- multiple of aNumber nearest self */
  2664.   case 174:            /* degreesToRadians */
  2665.   case 175:            /* radiansToDegrees */
  2666.  
  2667.   case 176:            /* sin */
  2668.   case 177:            /* cos */
  2669.   case 178:            /* tan */
  2670.   case 179:            /* arcSin */
  2671.   case 180:            /* arcCos */
  2672.   case 181:            /* arcTan */
  2673.     oop1 = popOOP();
  2674.     if (isClass(oop1, floatClass)) {
  2675.       failed = false;
  2676.       farg1 = floatOOPValue(oop1);
  2677.       switch (primitive) {
  2678.       case 176: pushOOP(floatNew(sin(farg1)));    break;
  2679.       case 177: pushOOP(floatNew(cos(farg1)));    break;
  2680.       case 178: pushOOP(floatNew(tan(farg1)));    break;
  2681.       case 179: pushOOP(floatNew(asin(farg1)));    break;
  2682.       case 180: pushOOP(floatNew(acos(farg1)));    break;
  2683.       case 181: pushOOP(floatNew(atan(farg1)));    break;
  2684.       }
  2685.     }
  2686.     if (failed) {
  2687.       unPop(1);
  2688.     }
  2689.     break;
  2690.  
  2691.   case 230:            /* SystemDictionary monitor: aBoolean */
  2692.     oop1 = popOOP();
  2693.     failed = false;
  2694. #ifdef USE_MONCONTROL    
  2695.     if (oop1 == trueOOP) {
  2696.       moncontrol(1);
  2697.     } else {
  2698.       moncontrol(0);
  2699.     }
  2700. #endif /* USE_MONCONTROL */
  2701.     break;
  2702.  
  2703.   case 231:            /* SystemDictionary gcMessage: aBoolean */
  2704.     oop1 = popOOP();
  2705.     failed = false;
  2706.     gcMessage = (oop1 == trueOOP);
  2707.     break;
  2708.  
  2709.   case 232:            /* SystemDictionary debug */
  2710.     failed = false;        /* used to allow dbx to stop based on st exec. */
  2711.     debug();
  2712.     break;
  2713.  
  2714.   case 233:            /* SystemDictionary verboseTrace: aBoolean */
  2715.     oop1 = popOOP();
  2716.     failed = false;
  2717.     verboseExecTracing = (oop1 == trueOOP);
  2718.     break;
  2719.     
  2720.   case 240:            /* SysFile class openFile: filename for: read-or-write */
  2721.     break;
  2722.  
  2723.   case 241:            /* SysFile close */
  2724.     break;
  2725.  
  2726.   case 242:            /* SysFile next */
  2727.     break;
  2728.  
  2729.   case 243:            /* SysFile nextPut: aCharOrByte */
  2730.     break;
  2731.  
  2732.   case 244:            /* SysFile atEnd */
  2733.     break;
  2734.  
  2735.   case 245:            /* SysFile position: anInteger */
  2736.     break;
  2737.  
  2738.   case 246:            /* SysFile size */
  2739.     break;
  2740.  
  2741.   case 247:            /* FileStream fileIn */
  2742.     oop1 = stackTop();
  2743.     fileStream = (FileStream)oopToObj(oop1);
  2744.     fileOOP = fileStream->file;
  2745.     file = (FILE *)cObjectValue(fileOOP);
  2746.     fileName = toCString(fileStream->name);
  2747.     if (access(fileName, 4) == 0) {
  2748.       failed = false;
  2749.       exportSP();
  2750.       initLexer(false);
  2751.       pushUNIXFile(file, fileName);
  2752.       yyparse();
  2753.       popStream(false);        /* we didn't open it, so we don't close it */
  2754.       importSP();
  2755.     }
  2756.     free(fileName);
  2757.     break;
  2758.  
  2759.   case 249:            /* Behavior makeDescriptorFor: funcNameString
  2760.                             returning: returnTypeSymbol
  2761.                         withArgs: argsArray */
  2762.     
  2763.     oop4 = popOOP();
  2764.     oop3 = popOOP();
  2765.     oop2 = popOOP();
  2766.     oop1 = popOOP();
  2767.     if (isClass(oop2, stringClass) && isClass(oop3, symbolClass)
  2768.     && (isClass(oop4, arrayClass)
  2769.         || isClass(oop4, undefinedObjectClass))) {
  2770.       failed = false;
  2771.       pushOOP(makeDescriptor(oop2, oop3, oop4));
  2772.     }
  2773.  
  2774.     if (failed) {
  2775.       unPop(4);
  2776.     }
  2777.     break;
  2778.  
  2779.   case 250:            /* Object snapshot */
  2780.     failed = false;
  2781.     saveToFile(defaultImageName);
  2782.     break;
  2783.  
  2784.   case 251:            /* Object snapshot: aString */
  2785.     oop2 = popOOP();
  2786.     if (isClass(oop2, stringClass)) {
  2787.       failed = false;
  2788.       fileName = toCString(oop2);
  2789.       saveToFile(fileName);
  2790.       free(fileName);
  2791.     }
  2792.  
  2793.     if (failed) {
  2794.       unPop(1);
  2795.     }
  2796.     break;
  2797.     
  2798.   case 252:            /* Object basicPrint */
  2799.     printf("Object: ");
  2800.     printObject(stackTop());
  2801.     printf("\n");
  2802.     failed = false;
  2803.     break;
  2804.  
  2805.   case 253:            /* Behavior compileString: aString */
  2806.     oop2 = popOOP();
  2807.     oop1 = stackTop();
  2808.     if (isClass(oop2, stringClass)) {
  2809.       failed = false;
  2810.       exportSP();
  2811.       initLexer(true);
  2812.       pushSmalltalkString(oop2);
  2813.       setCompilationClass(oop1);
  2814.       yyparse();
  2815.       popStream(false);        /* don't close a String! */
  2816.       importSP();
  2817.     }
  2818.     if (failed) {
  2819.       unPop(1);
  2820.     }
  2821.     break;
  2822.  
  2823.   case 254:            /* IO primitive, variadic */
  2824.     for (i = numArgs; --i >= 0; ) {
  2825.       oopVec[i] = popOOP();
  2826.     }
  2827.     oop1 = stackTop();
  2828.     if (isInt(oopVec[0])) {
  2829.       failed = false;
  2830.       arg1 = toInt(oopVec[0]);
  2831.       if (arg1 == ENUM_INT(openFilePrim) || arg1 == ENUM_INT(popenFilePrim)) {
  2832.     /* open: fileName[1] mode: mode[2] or popen: command[1] dir: direction[2] */
  2833.     fileName = toCString(oopVec[1]);
  2834.     fileMode = toCString(oopVec[2]);
  2835.     if (arg1 == ENUM_INT(openFilePrim)) {
  2836.       file = fopen((char *)fileName, (char *)fileMode);
  2837.     } else {
  2838.       file = popen((char *)fileName, (char *)fileMode);
  2839.     }
  2840.     if (file == NULL) {
  2841.       errorf("Failed to open %s named '%s'",
  2842.          (ENUM_INT(openFilePrim) == arg1) ? "file" : "pipe",
  2843.          fileName);
  2844.       free(fileName);
  2845.       free(fileMode);
  2846.       failed = true;
  2847.       break;
  2848.     }
  2849.       
  2850.     fileOOP = cObjectNew(file);
  2851.     setFileStreamFile(oop1, fileOOP, oopVec[1]);
  2852.     free(fileName);
  2853.     free(fileMode);
  2854.       } else {
  2855.     fileStream = (FileStream)oopToObj(oop1);
  2856.     fileOOP = fileStream->file;
  2857.     file = (FILE *)cObjectValue(fileOOP);
  2858.     switch (arg1) {
  2859.  
  2860.     case closeFilePrim:    /* FileStream close */
  2861.       fclose(file);
  2862.       break;
  2863.     
  2864.     case getCharPrim:    /* FileStream next */
  2865.       /* ### handle returning eof here */
  2866.       ch = getc(file);
  2867.       setStackTop(charOOPAt(ch));
  2868.       break;
  2869.       
  2870.     case putCharPrim:    /* FileStream nextPut: aChar */
  2871.       if (isClass(oopVec[1], charClass)) {
  2872.         ch = charOOPValue(oopVec[1]);
  2873.         fputc(ch, file);
  2874.       } else {
  2875.         failed = true;
  2876.       }
  2877.       break;
  2878.  
  2879.     case seekPrim:        /* FileStream position: position */
  2880.       fseek(file, toInt(oopVec[1]), 0);
  2881.       break;
  2882.  
  2883.     case tellPrim:        /* FileStream position */
  2884.       setStackTop(fromInt(ftell(file)));
  2885.       break;
  2886.  
  2887.     case eofPrim:        /* FileStream atEnd */ 
  2888.       popOOP();        /* remove self */
  2889.       ch = fgetc(file);
  2890.       atEof = feof(file);
  2891.       pushBoolean(atEof);
  2892.       ungetc(ch, file);
  2893.       break;
  2894.  
  2895.     case sizePrim:
  2896.       if (fstat(fileno(file), &statBuf)) {
  2897.         failed = true;
  2898.       } else {
  2899.         setStackTop(fromInt(statBuf.st_size));
  2900.       }
  2901.       break;
  2902.     }
  2903.       }
  2904.     }
  2905.  
  2906.     if (failed) {
  2907.       unPop(numArgs);
  2908.     }
  2909.     break;
  2910.  
  2911.   case 255:            /* C callout primitive */
  2912.     inInterpreter = false;
  2913.     exportSP();
  2914.     inCCode = true;
  2915.     if (setjmp(cCalloutJmpBuf) == 0) {
  2916.       invokeCRoutine(numArgs, methodOOP);
  2917.     }
  2918.     inCCode = false;
  2919.     importSP();
  2920.     inInterpreter = true;
  2921.     failed = false;
  2922.     break;
  2923.  
  2924.   default:
  2925.     errorf("Unhandled primitive operation %d", primitive);
  2926.   }
  2927.  
  2928.   exportSP();
  2929.   return (failed);
  2930.  
  2931. }
  2932.  
  2933. /*
  2934. These are the primitives as defined in the Blue Book.  The ones with numbers
  2935. but without stars are those which are not implemented.
  2936.  
  2937. * 1 +
  2938. * 2 -
  2939. * 3 <
  2940. * 4 >
  2941. * 5 <=
  2942. * 6 >=
  2943. * 7 =
  2944. * 8 ~=
  2945. * 9 *
  2946. * 10 /
  2947. * 11 \\
  2948. * 12 //
  2949. * 13 quo:
  2950. * 14 bitAnd:
  2951. * 15 bitOr:
  2952. * 16 bitXor:
  2953. * 17 bitShift:
  2954.  
  2955. * 40 Smallinteger asFloat
  2956. * 41 Float +
  2957. * 42 Float -
  2958. * 43 Float >
  2959. * 44 Float <
  2960. * 45 Float <=
  2961. * 46 Float >=
  2962. * 47 Float =
  2963. * 48 Float ~=
  2964. * 49 Float *
  2965. * 50 Float /
  2966. * 51 Float truncated
  2967. * 52 Float fractionPart
  2968. * 53 Float exponent
  2969. * 54 Float timesTwoPower:
  2970.  
  2971. * 60 Object at:
  2972.    Object basicAt:
  2973. * 61 Object basicAt:put:
  2974.    Object at:put:
  2975. * 62 Object basicSize
  2976.    Object size
  2977.    String size
  2978.    ArrayedCollection size
  2979. * 63 String at:
  2980.    String basicAt:
  2981. * 64 String basicAt:put:
  2982.    String at:put:
  2983.  
  2984. * 70 Behavior basicNew
  2985.    Behavior new
  2986.    Interval class new
  2987. * 71 Behavior new:
  2988.    Behavior basicNew:
  2989. * 72 Object become:
  2990. * 73 Object instVarAt:
  2991. * 74 Object instVarAt:put:
  2992. * 75 Object asOop
  2993.    Object hash
  2994.    Symbol hash
  2995. * 76 SmallInteger asObject
  2996.    SmallInteger asObjectNoFail
  2997. * 77 Behavior someInstance
  2998. * 78 Object nextInstance
  2999. 79 CompiledMethod class newMethod:header:
  3000. * 80 ContextPart blockCopy:
  3001. * 81 BlockContext value:value:value:
  3002.    BlockContext value:
  3003.    BlockContext value:value:
  3004. * 82 BlockContext valueWithArguments:
  3005. * 83 Object perform:with:with:with:
  3006.    Object perform:with:
  3007.    Object perform:with:with:
  3008.    Object perform:
  3009. * 84 Object perform:withArguments:
  3010.  
  3011. 105 ByteArray primReplaceFrom:to:with:startingAt:
  3012.     ByteArray replaceFrom:to:withString:startingAt:
  3013.     String replaceFrom:to:withByteArray:startingAt:
  3014.     String primReplaceFrom:to:with:startingAt:
  3015.  
  3016. * 110 Character =
  3017.     Object ==
  3018. * 111 Object class
  3019.  
  3020. */
  3021.  
  3022. static OOP getActiveProcess()
  3023. {
  3024.   ProcessorScheduler processor;
  3025.  
  3026.   if (!isNil(switchToProcess)) {
  3027.     return (switchToProcess);
  3028.   } else {
  3029.     processor = (ProcessorScheduler)oopToObj(processorOOP);
  3030.     return (processor->activeProcess);
  3031.   }
  3032. }
  3033.  
  3034. static void addLastLink(semaphoreOOP, processOOP)
  3035. OOP    semaphoreOOP, processOOP;
  3036. {
  3037.   Semaphore    sem;
  3038.   Process    process, lastProcess;
  3039.   OOP        lastProcessOOP;
  3040.  
  3041.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3042.   process = (Process)oopToObj(processOOP);
  3043.  
  3044.   process->nextLink = nilOOP;
  3045.   prepareToStore(processOOP, semaphoreOOP);
  3046.   process->myList = semaphoreOOP;
  3047.   if (isNil(sem->lastLink)) {
  3048.     prepareToStore(semaphoreOOP, processOOP);
  3049.     sem->firstLink = sem->lastLink = processOOP;
  3050.   } else {
  3051.     lastProcessOOP = sem->lastLink;
  3052.     lastProcess = (Process)oopToObj(lastProcessOOP);
  3053.     prepareToStore(lastProcessOOP, processOOP);
  3054.     lastProcess->nextLink = processOOP;
  3055.     prepareToStore(semaphoreOOP, processOOP);
  3056.     sem->lastLink = processOOP;
  3057.   }
  3058. }
  3059.  
  3060. syncSignal(semaphoreOOP)
  3061. OOP    semaphoreOOP;
  3062. {
  3063.   Semaphore sem;
  3064.  
  3065.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3066.   if (isEmpty(semaphoreOOP)) {    /* nobody waiting */
  3067.     sem->signals = incrInt(sem->signals);
  3068.   } else {
  3069.     resumeProcess(removeFirstLink(semaphoreOOP));
  3070.   }
  3071. }
  3072.  
  3073. static OOP removeFirstLink(semaphoreOOP)
  3074. OOP    semaphoreOOP;
  3075. {
  3076.   Semaphore    sem;
  3077.   Process    process;
  3078.   OOP        processOOP;
  3079.  
  3080.   sem = (Semaphore)oopToObj(semaphoreOOP);
  3081.   processOOP = sem->firstLink;
  3082.   process = (Process)oopToObj(processOOP);
  3083.   prepareToStore(semaphoreOOP, processOOP);
  3084.   sem->firstLink = process->nextLink;
  3085.   if (isNil(sem->firstLink)) {
  3086.     sem->lastLink = nilOOP;
  3087.   }
  3088.  
  3089.   process->nextLink = nilOOP;
  3090.   process->myList = nilOOP;
  3091.  
  3092.   return (processOOP);
  3093. }
  3094.  
  3095. static void resumeProcess(processOOP)
  3096. OOP    processOOP;
  3097. {
  3098.   OOP        activeOOP;
  3099.   Process    process, active;
  3100.  
  3101.   activeOOP = getActiveProcess();
  3102.   active = (Process)oopToObj(activeOOP);
  3103.   process = (Process)oopToObj(processOOP);
  3104.  
  3105.   if (toInt(process->priority) > toInt(active->priority)) {
  3106.     /*
  3107.      * we're resuming a process with a higher priority, so sleep the
  3108.      * current one and activate the new one
  3109.      */
  3110.     sleepProcess(activeOOP);
  3111.     activateProcess(processOOP);
  3112.   } else {
  3113.     /* this process isn't higher than the active one */
  3114.     sleepProcess(processOOP);
  3115.   }
  3116. }
  3117.  
  3118. static void activateProcess(processOOP)
  3119. OOP    processOOP;
  3120. {
  3121.   switchToProcess = processOOP;
  3122.   exceptFlag = true;
  3123. }
  3124.  
  3125. static void sleepProcess(processOOP)
  3126. OOP    processOOP;
  3127. {
  3128.   Process    process;
  3129.   int        priority;
  3130.   OOP        processLists;
  3131.   OOP        processList;
  3132.  
  3133.   process = (Process)oopToObj(processOOP);
  3134.   priority = toInt(process->priority);
  3135.   processLists = getProcessLists();
  3136.   processList = arrayAt(processLists, priority);
  3137.  
  3138.   /* add process to end of priority queue */
  3139.   addLastLink(processList, processOOP);
  3140. }
  3141.  
  3142.  
  3143. static void suspendActiveProcess()
  3144. {
  3145.   activateProcess(highestPriorityProcess());
  3146. }
  3147.  
  3148.  
  3149. static OOP highestPriorityProcess()
  3150. {
  3151.   OOP        processLists, processList;
  3152.   int        priority;
  3153.  
  3154.   processLists = getProcessLists();
  3155.   priority = numOOPs(oopToObj(processLists));
  3156.   for (; priority > 0 ; priority--) {
  3157.     processList = arrayAt(processLists, priority);
  3158.     if (!isEmpty(processList)) {
  3159.       return (removeFirstLink(processList));
  3160.     }
  3161.   }
  3162.  
  3163.   errorf("No Runnable process!!!");
  3164.   exit(0);
  3165. }
  3166.  
  3167. static Boolean isEmpty(processListOOP)
  3168. OOP    processListOOP;
  3169. {
  3170.   Semaphore    processList;
  3171.  
  3172.   processList = (Semaphore)oopToObj(processListOOP);
  3173.   return (isNil(processList->firstLink));
  3174. }
  3175.  
  3176. static OOP getProcessLists()
  3177. {
  3178.   ProcessorScheduler processor;
  3179.  
  3180.   processor = (ProcessorScheduler)oopToObj(processorOOP);
  3181.   return (processor->processLists);
  3182. }
  3183.  
  3184. static OOP semaphoreNew()
  3185. {
  3186.   Semaphore    sem;
  3187.  
  3188.   sem = (Semaphore)instantiate(semaphoreClass);
  3189.   sem->signals = fromInt(0);
  3190.  
  3191.   return (allocOOP(sem));
  3192. }
  3193.  
  3194.  
  3195. /*
  3196.  *    static void methodHasBlockContext(methodOOP)
  3197.  *
  3198.  * Description
  3199.  *
  3200.  *    Marks a method context has having created a block context.
  3201.  *
  3202.  * Inputs
  3203.  *
  3204.  *    methodOOP: MethodContext OOP to be marked
  3205.  *        
  3206.  *
  3207.  */
  3208. static void methodHasBlockContext(methodOOP)
  3209. OOP    methodOOP;
  3210. {
  3211.   MethodContext methodContext;
  3212.  
  3213.   methodContext = (MethodContext)oopToObj(methodOOP);
  3214.   /* Since trueOOP is in the root set, we don't have to prepare to store it */
  3215.   methodContext->hasBlock = trueOOP;
  3216. }
  3217.  
  3218. void setFileStreamFile(fileStreamOOP, fileOOP, fileNameOOP)
  3219. OOP    fileStreamOOP, fileOOP, fileNameOOP;
  3220. {
  3221.   FileStream    fileStream;
  3222.  
  3223.   fileStream = (FileStream)oopToObj(fileStreamOOP);
  3224.   prepareToStore(fileStreamOOP, fileOOP);
  3225.   fileStream->file = fileOOP;
  3226.   prepareToStore(fileStreamOOP, fileNameOOP);
  3227.   fileStream->name = fileNameOOP;
  3228. }
  3229.  
  3230.  
  3231. /*
  3232.  *    static void sendBlockValue(numArgs)
  3233.  *
  3234.  * Description
  3235.  *
  3236.  *    This is the equivalent of sendMessage, but is for blocks.  The block
  3237.  *    context that is to the the receiver of the "value" message should be
  3238.  *    "numArgs" into the stack.  Temporaries come from the block's method
  3239.  *    context, as does self.  IP is set to the proper
  3240.  *    place within the block's method's byte codes, and SP is set to the top
  3241.  *    of the arguments in the block context, which have been copied out of
  3242.  *    the caller's context.
  3243.  *
  3244.  * Inputs
  3245.  *
  3246.  *    numArgs: 
  3247.  *        The number of arguments sent to the block.
  3248.  *
  3249.  */
  3250. static void sendBlockValue(numArgs)
  3251. int    numArgs;
  3252. {
  3253.   OOP        selector, blockContextOOP, methodContextOOP;
  3254.   BlockContext    blockContext;
  3255.   MethodContext thisContext, methodContext;
  3256.   int        i;
  3257.  
  3258.   if (!isNil(thisContextOOP)) {
  3259.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3260.     /* save old context information */
  3261.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  3262.     /* leave sp pointing to receiver, which is replaced on return with value*/
  3263.     thisContext->spOffset = fromInt(sp - numArgs - thisContext->contextStack);
  3264.   }
  3265.  
  3266.   /* prepare the new state */
  3267.   blockContextOOP = stackAt(numArgs);
  3268.   maybeMoveOOP(blockContextOOP); /* make sure we're alive */
  3269.  
  3270.   blockContext = (BlockContext)oopToObj(blockContextOOP);
  3271.   maybeMoveOOP(thisContextOOP);    /* ### not sure if this is needed*/
  3272.   blockContext->caller = thisContextOOP;
  3273.   switch (numArgs) {
  3274.   case 0:    blockContext->selector = valueSymbol; break;
  3275.   case 1:    blockContext->selector = valueColonSymbol; break;
  3276.   case 2:    blockContext->selector = valueColonValueColonSymbol; break;
  3277.   case 3:    blockContext->selector = valueColonValueColonValueColonSymbol; break;
  3278.   default:    blockContext->selector = valueWithArgumentsColonSymbol; break;
  3279.   }
  3280.  
  3281.   /* home is set when the block is created */
  3282.  
  3283.   /* copy numArgs arguments into new context */
  3284.   memcpy(blockContext->contextStack, &sp[-numArgs+1],
  3285.      (numArgs) * sizeof(OOP));
  3286.   for (i = 0; i < numArgs; i++) {
  3287.     maybeMoveOOP(blockContext->contextStack[i]);
  3288.   }
  3289.  
  3290.   sp = &blockContext->contextStack[numArgs-1]; /* start of stack-1 */
  3291.  
  3292.   thisContextOOP = blockContextOOP;
  3293.  
  3294.   methodContextOOP = blockContext->home;
  3295.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  3296.   ip = toInt(blockContext->initialIP) + getMethodByteCodes(methodContext->method);
  3297.   thisMethod = methodContext->method;
  3298.   maybeMoveOOP(thisMethod);
  3299.   temporaries = methodContext->contextStack;
  3300.   self = methodContext->receiver;
  3301.   maybeMoveOOP(self);
  3302. }
  3303.  
  3304. /*
  3305.  *    static char *selectorAsString(selector)
  3306.  *
  3307.  * Description
  3308.  *
  3309.  *    Converts a selector to a C string object
  3310.  *
  3311.  * Inputs
  3312.  *
  3313.  *    selector: A OOP for the selector, a Symbol.
  3314.  *        
  3315.  *
  3316.  * Outputs
  3317.  *
  3318.  *    C string that corresponds to the selector's printed name.
  3319.  */
  3320. static char *selectorAsString(selector)
  3321. OOP    selector;
  3322. {
  3323.   return (symbolAsString(selector));
  3324. }
  3325.  
  3326. /*
  3327.  *    static OOP findMethod(receiverClass, selector)
  3328.  *
  3329.  * Description
  3330.  *
  3331.  *    Scans the methods of "receiverClass" and all its super classes for one
  3332.  *    with selector "selector".  It returns the method if it found, otherwise
  3333.  *    nil is returned.
  3334.  *
  3335.  * Inputs
  3336.  *
  3337.  *    receiverClass:
  3338.  *        The class to begin the search in.  This is normally called from
  3339.  *        the message sending code, so that's why this parameter is
  3340.  *        called receiverClass.
  3341.  *    selector:
  3342.  *        The selector for the method that is being sought.
  3343.  *    methodClassPtr:
  3344.  *        The class that the method was eventually found in.  Passed
  3345.  *        by reference and set when returning.
  3346.  *
  3347.  * Outputs
  3348.  *
  3349.  *    Method for "selector", or nilOOP if not found.  "methodClassPtr" is
  3350.  *    returned as a by-reference parameter.
  3351.  */
  3352. static OOP findMethod(receiverClass, selector, methodClassPtr)
  3353. OOP    receiverClass, selector, *methodClassPtr;
  3354. {
  3355.   OOP        classOOP, methodOOP;
  3356.  
  3357.   for (classOOP = receiverClass; !isNil(classOOP);
  3358.        classOOP = superClass(classOOP)) {
  3359.     methodOOP = findClassMethod(classOOP, selector);
  3360.     if (!isNil(methodOOP)) {
  3361.       *methodClassPtr = classOOP;
  3362.       return (methodOOP);
  3363.     }
  3364.   }
  3365.  
  3366.   *methodClassPtr = undefinedObjectClass; /* probably not used */
  3367.   return (nilOOP);
  3368. }
  3369.  
  3370. /* runs before GC turned on */
  3371. void initProcessSystem()
  3372. {
  3373.   OOP        processLists;
  3374.   int        i;
  3375.   ProcessorScheduler processor;
  3376.   Process    initialProcess;
  3377.   OOP        initialProcessOOP;
  3378.   
  3379.  
  3380.   processLists = arrayNew(NUM_PRIORITIES);
  3381.  
  3382.   for (i = 1; i <= NUM_PRIORITIES; i++) {
  3383.     arrayAtPut(processLists, i, semaphoreNew()); /* ### should be linked list */
  3384.   }
  3385.  
  3386.   initialProcess = (Process)instantiate(processClass);
  3387.   initialProcess->priority = fromInt(4);
  3388.   initialProcessOOP = allocOOP(initialProcess);
  3389.  
  3390.  
  3391.   processor = (ProcessorScheduler)instantiate(processorSchedulerClass);
  3392.   processor->processLists = processLists;
  3393.   processor->activeProcess = initialProcessOOP;
  3394.  
  3395.   processorOOP = allocOOP(processor);
  3396. }
  3397.  
  3398.  
  3399. void initInterpreter()
  3400. {
  3401.   thisContextOOP = nilOOP;
  3402.   asyncQueueIndex = 0;
  3403.   switchToProcess = nilOOP;
  3404. }
  3405.  
  3406. void prepareExecutionEnvironment()
  3407. {
  3408.   MethodContext thisContext, newContext;
  3409.   OOP        newContextOOP;
  3410.  
  3411.   abortExecution = false;
  3412.  
  3413.   if (!isNil(thisContextOOP)) {
  3414.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3415.     /* save old context information */
  3416.     thisContext->ipOffset = fromInt(relativeByteIndex(ip, thisMethod));
  3417.     /* leave sp pointing to receiver, which is replaced on return with value*/
  3418.     thisContext->spOffset = fromInt(sp - thisContext->contextStack);
  3419.   }
  3420.  
  3421.   /* now make a dummy context to run with */
  3422.   newContextOOP = allocMethodContext();
  3423.   newContext = (MethodContext)oopToObj(newContextOOP);
  3424.   ip = nil;
  3425.   maybeMoveOOP(thisContextOOP);
  3426.   newContext->sender = thisContextOOP;
  3427.   thisMethod = newContext->method = nilOOP;
  3428.   newContext->selector = nilOOP; /* no real selector invoked us */
  3429.   newContext->receiver = nilOOP; /* make self be real (well, nil) */
  3430.   sp = newContext->contextStack - 1;
  3431.  
  3432.   temporaries = newContext->contextStack;
  3433.   self = nilOOP;
  3434.  
  3435.   thisContextOOP = newContextOOP;
  3436.  
  3437.   invalidateMethodCache();
  3438. #ifdef countingByteCodes 
  3439.   initByteCodeCounter();
  3440. #endif
  3441. }
  3442.  
  3443. OOP finishExecutionEnvironment()
  3444. {
  3445.   MethodContext oldContext, thisContext;
  3446.   OOP        oldContextOOP, returnedValue;
  3447.   
  3448.   returnedValue = stackTop();
  3449.   oldContextOOP = thisContextOOP;
  3450.   oldContext = (MethodContext)oopToObj(oldContextOOP);
  3451.   thisContextOOP = oldContext->sender;
  3452.  
  3453.   if (!isNil(thisContextOOP)) {
  3454.     maybeMoveOOP(thisContextOOP);
  3455.     thisContext = (MethodContext)oopToObj(thisContextOOP);
  3456.     /* restore old context information */
  3457.     thisMethod = thisContext->method;
  3458.     maybeMoveOOP(thisMethod);
  3459.     temporaries = thisContext->contextStack;
  3460.     self = thisContext->receiver;
  3461.     maybeMoveOOP(self);
  3462.     ip = toInt(thisContext->ipOffset) + getMethodByteCodes(thisMethod);
  3463.     sp = thisContext->contextStack + toInt(thisContext->spOffset);
  3464.   }
  3465.   return (returnedValue);
  3466. }
  3467.  
  3468. static void invalidateMethodCache()
  3469. {
  3470.   register int    i;
  3471.  
  3472.   cacheHits = cacheMisses = 0;
  3473.  
  3474.   for (i = 0; i < METHOD_CACHE_SIZE; i++) {
  3475.     methodCacheSelectors[i] = primitiveCacheSelectors[i] = nilOOP;
  3476.     methodCacheClasses[i] = primitiveCacheClasses[i] = nilOOP;
  3477.     methodCacheMethods[i] = nilOOP;
  3478.     collide[0] = primitiveCachePrimitives[i] = 0;
  3479.   }
  3480. }
  3481.  
  3482. void updateMethodCache(selectorOOP, classOOP, methodOOP)
  3483. OOP    selectorOOP, classOOP, methodOOP;
  3484. {
  3485.   long        hashIndex;
  3486.  
  3487.   hashIndex = ((long)selectorOOP ^ (long)classOOP) >> 4;
  3488.   hashIndex &= (METHOD_CACHE_SIZE - 1);
  3489.   if (methodCacheSelectors[hashIndex] == selectorOOP &&
  3490.       methodCacheClasses[hashIndex] == classOOP) {
  3491.     methodCacheMethods[hashIndex] = methodOOP;
  3492.   }
  3493. }
  3494.  
  3495. #ifdef countingByteCodes
  3496. initByteCodeCounter()
  3497. {
  3498.   int i;
  3499.  
  3500.   for (i = 0; i < 256; i++) {
  3501.     primitives[i] = byteCodes[i] = 0;
  3502.   }
  3503. }
  3504.  
  3505. printByteCodeCounts()
  3506. {
  3507.   int i;
  3508.  
  3509.   for (i = 0; i < 256; i++) {
  3510.     if (byteCodes[i]) {
  3511.       printf("Byte code %d = %d\n", i, byteCodes[i]);
  3512.     }
  3513.   }
  3514.  
  3515.   printf("\n---> primitives:\n");
  3516.   for (i = 0; i < 256; i++) {
  3517.     if (primitives[i]) {
  3518.       printf("Primitive %d = %d\n", i, primitives[i]);
  3519.     }
  3520.   }
  3521.  
  3522. }
  3523. #endif
  3524.  
  3525. relativeByteIndex(bp, methodOOP)
  3526. Byte    *bp;
  3527. OOP    methodOOP;
  3528. {
  3529.   return (bp - getMethodByteCodes(methodOOP));
  3530. }
  3531.  
  3532. /*
  3533.  *    void moveProcessorRegisters()
  3534.  *
  3535.  * Description
  3536.  *
  3537.  *    Part of the GC flip, copy the root set process.  This ensures that the
  3538.  *    processor registers are pointing to objects in new space.  The term
  3539.  *    "processor registers" refers here to interpreter variables like ip, sp,
  3540.  *    temporaries, etc.
  3541.  *
  3542.  */
  3543. void moveProcessorRegisters()
  3544. {
  3545.   MethodContext thisContext;    /* may be block context, but doesn't matter */
  3546.   MethodContext    methodContext;
  3547.   int        spOffset, ipOffset;
  3548.   OOP        methodContextOOP;
  3549.  
  3550.   invalidateMethodCache();
  3551.  
  3552.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  3553.   spOffset = sp - thisContext->contextStack;
  3554.   ipOffset = relativeByteIndex(ip, thisMethod);
  3555.  
  3556.   maybeMoveOOP(thisContextOOP);
  3557.   maybeMoveOOP(thisMethod);
  3558.  
  3559.   ip = ipOffset + getMethodByteCodes(thisMethod);
  3560.  
  3561.   thisContext = (MethodContext)oopToObj(thisContextOOP);
  3562.   sp = thisContext->contextStack + spOffset;
  3563.  
  3564.   methodContextOOP = getMethodContext(thisContextOOP);
  3565.   maybeMoveOOP(methodContextOOP);
  3566.   methodContext = (MethodContext)oopToObj(methodContextOOP);
  3567.  
  3568.   temporaries = methodContext->contextStack;
  3569. /*** just as a test...I don't remember why the comment says self remains valid,
  3570.  *** because in my tests, it's pointing to old-space data...for long running
  3571.  *** executions, this could be disasterous 
  3572.  */
  3573.   /* self remains valid */
  3574. maybeMoveOOP(self);
  3575.  
  3576.   moveSemaphoreOOPs();
  3577. }
  3578.  
  3579. /*
  3580.  *    static void moveSemaphoreOOPs()
  3581.  *
  3582.  * Description
  3583.  *
  3584.  *    This routine doesn't really do anything yet.  It's intended purpose is
  3585.  *    to be called during the root set copying part of a GC flip to copy any
  3586.  *    asynchronous semaphores.  However, the async semaphore representation
  3587.  *    is likely not to be in terms of Smalltalk objects for a variety of
  3588.  *    reasons, so the need for this routine may never materialize.
  3589.  *
  3590.  */
  3591. static void moveSemaphoreOOPs()
  3592. {
  3593.   int        i;
  3594.   IntState    oldSigMask;
  3595.  
  3596.   oldSigMask = disableInterrupts(); /* block out everything! */
  3597.   /* ### this needs to be changed; async signals shouldn't be oops! */
  3598.   for (i = 0; i < asyncQueueIndex; i++) {
  3599.     moveOOP(queuedAsyncSignals[i]);
  3600.   }
  3601.   enableInterrupts(oldSigMask);
  3602. }
  3603.  
  3604. /*
  3605.  *    void initSignals()
  3606.  *
  3607.  * Description
  3608.  *
  3609.  *    Trap the signals that we care about, basically SIGBUG and SIGSEGV.
  3610.  *    These are sent to the back trace routine so we can at least have some
  3611.  *    idea of where we were when we died.
  3612.  *
  3613.  */
  3614. void initSignals()
  3615. {
  3616.   signal(SIGBUS, interruptHandler);
  3617.   signal(SIGSEGV, interruptHandler);
  3618.  
  3619.   signal(SIGINT, stopExecuting);
  3620. }
  3621.  
  3622.  
  3623. /*
  3624.  *    static signalType stopExecuting(sig)
  3625.  *
  3626.  * Description
  3627.  *
  3628.  *    Sets flags so that the interpreter starts returning immediately
  3629.  *    from whatever byte codes it's executing.  It returns via normal method
  3630.  *    returns, so that the world is in a consistent state when it's done.
  3631.  *
  3632.  * Inputs
  3633.  *
  3634.  *    sig   : signal that caused the interrupt (typically ^C), or 0, which
  3635.  *        comes from a call from within the system.
  3636.  *
  3637.  */
  3638. static signalType stopExecuting(sig)
  3639. {
  3640.   if (sig) {
  3641.     printf("\nInterrupt!\n");
  3642.   }
  3643.  
  3644.   abortExecution = true;
  3645.   exceptFlag = true;
  3646.   if (inCCode) {
  3647.     longjmp(cCalloutJmpBuf, 1);    /* throw out from C code */
  3648.   }
  3649. }
  3650.  
  3651.  
  3652. /*
  3653.  *    static signalType interruptHandler(sig)
  3654.  *
  3655.  * Description
  3656.  *
  3657.  *    Called to handle serious problems, such as segmentation violation.
  3658.  *    Tries to show a method invocation backtrace if possibly, otherwise
  3659.  *    tries to show where the system was in the file it was procesing when
  3660.  *    the error occurred.
  3661.  *
  3662.  * Inputs
  3663.  *
  3664.  *    sig   : Signal number, an integer
  3665.  *
  3666.  * Outputs
  3667.  *
  3668.  *    not used.  Always exits from Smalltalk.
  3669.  */
  3670. static signalType interruptHandler(sig)
  3671. int    sig;
  3672. {
  3673.   switch (sig) {
  3674.   case SIGBUS:
  3675.     errorf("Bus Error");
  3676.     break;
  3677.  
  3678.   case SIGSEGV:
  3679.     errorf("Segmentation violation");
  3680.     break;
  3681.     
  3682.   default:
  3683.     errorf("Unknown signal caught: %d", sig);
  3684.   }
  3685.  
  3686.   if (makeCoreFile) {
  3687.     kill(getpid(), SIGQUIT);    /* die with a core dump */
  3688.   }
  3689.  
  3690.   if (inInterpreter) {
  3691.     showBacktrace();
  3692.   } else {
  3693.     errorf("Not in interpreter!!");
  3694.   }
  3695.  
  3696.   exit(1);
  3697. }
  3698.  
  3699. static void showBacktrace()
  3700. {
  3701.   OOP        context, receiver, receiverClass;
  3702.   MethodContext methodContext;
  3703.  
  3704.   for (context = thisContextOOP; !isNil(context);
  3705.        context = methodContext->sender) {
  3706.     if (!isRealOOP(context)) {
  3707.       printf("Context stack corrupted!\n");
  3708.       break;
  3709.     }
  3710.     methodContext = (MethodContext)oopToObj(context);
  3711.     if (!isRealOOP(methodContext->selector)) {
  3712.       printf("Context stack corrupted!\n");
  3713.       break;
  3714.     }
  3715.       
  3716.     receiver = methodContext->receiver;
  3717.     if (isInt(receiver)) {
  3718.       receiverClass = integerClass;
  3719.     } else {
  3720.       if (!isRealOOP(receiver)) {
  3721.     printf("Context stack corrupted!\n");
  3722.     break;
  3723.       }
  3724.       receiverClass = oopClass(receiver);
  3725.     }
  3726.     printObject(receiverClass);
  3727.     printf(">>");
  3728.     printObject(methodContext->selector);
  3729.     printf("\n");
  3730.   }
  3731.  
  3732. }
  3733.  
  3734. static Boolean isRealOOP(oop)
  3735. OOP oop;
  3736. {
  3737.   return (oop >= oopTable && oop < &oopTable[TOTAL_OOP_TABLE_SLOTS]);
  3738. }
  3739.